#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main() {
	int p,q;
	while(cin >> p >> q) {
		vector<int> v;
		int m;
		p %= q;
		p *= 10;
		while(find(v.begin(), v.end(), p) == v.end()) {
			v.push_back(p);
			p %= q;
			p *= 10;
		}
		if(v[v.size()-1] == 0) {
			rep(i,v.size()-1) {
				cout << v[i]/q;
			}
			cout << endl;
		} else {
			rep(i,v.size()) {
				cout << v[i]/q;
			}
			cout << endl;
			bool f = 0;
			for(auto it = v.begin(); it != v.end(); ++it) {
				if(*it == p) {
					f = 1;
				}
				if(f) cout << "^";
				else cout << " ";
			}
			cout << endl;
		}
	}
	return 0;
}