#include <string>
#include <iostream>
using namespace std;
int p, q, visit[1000000];
int main() {
	while (cin >> p >> q) {
		string s;
		for (int i = 0; i < q; i++) visit[i] = -1;
		for (int i = 0; ; i++) {
			p *= 10, s += p / q + 48, p %= q;
			if (p == 0) {
				cout << s << endl; s = "-1"; break;
			}
			if (visit[p] != -1) {
				cout << s << endl;
				for (int j = 0; j <= visit[p]; j++) cout << ' ';
				for (int j = visit[p] + 1; j <= i; j++) cout << '^';
				cout << endl; break;
			}
			visit[p] = i;
		}
	}
}