#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

int main() {
	int p, q;
	
	while (cin >> p >> q) {
		string ans;
		map<int, int> m;
		int pos = -1;
		
		m[p % q] = 0;
		p *= 10;
		FOR(i, 1, 100) {
			int a = p / q, b = p % q;
			
			ans += (char)(a + '0');
			
			if (b == 0) break;
			if (m.find(b) != m.end()) {
				pos = m[b];
				break;
			}
			m[b] = i;
			
			p = b * 10;
		}
		
		cout << ans << endl;
		
		if (pos != -1) {
			REP(i, pos) putchar(' ');
			FOR(i, pos, ans.size()) putchar('^');
			puts("");
		}
		
	}
	return 0;
}