#include<iostream>
#include<cstring>
using namespace std;

long long p, a, cnt;
long long x[100000];

int main() {
	while (cin >> p >> a) {
		cnt = 0;
		memset(x, 0, sizeof(x));
		while (true) {
			if (cnt >= 1) { cout << p / a; }
			if (p%a == 0) { break; }
			p *= 10;
			p %= (a * 10);
			x[cnt] = p;
			cnt++;
			for (int i = 0; i < cnt - 1; i++) {
				if (p == x[i]) {
					cout << endl;
					for (int j = 1; j <= i; j++) {
						cout << ' ';
					}
					for (int j = i + 1; j < cnt; j++) {
						cout << '^';
					}
					goto E;
				}
			}
		}
	E:;
		cout << endl;
	}
	return 0;
}