#include <iostream>
#include <cstdio>
#include <map>

using namespace std;

map<int, int> used;

void solve(int a, int b, int times) {
	int r = a % b;

	if (used[r]) {
		cout << (a / b) << endl;

		--used[r], ++times;
		for (int i = 0; i < used[r]; ++i) cout << " ";
		for (int i = used[r]; i < times; ++i) cout << "^";
		cout << endl;
	}
	else {
		used[r] = times+2;
		cout << (a / b);
		if (r) solve(r * 10, b, times + 1);
		else cout << endl;
	}
}

int main() {
	int n, m;
	while (scanf("%d %d\n", &n, &m) != EOF) {
		used.clear();
		used[n % m] = 1;
		solve(n * 10, m, 0);
	}
}