#include <iostream>
#include <map>
#include <algorithm>

using namespace std;

const int N = 80;
int ans[N] = { 0 };
int damari[N] = { 0 };
map<int, bool> amari;

int main(){
	int p, q, r, c, i;
	bool loop;
	while(cin >> p >> q){
		loop = false;
		p *= 10;
		for(i = 0;;++i){
			c = 0;
			while(p >= q){
				p -= q;
				++c;
			}
			ans[i] = c;
			if(p == 0) break;
			damari[i] = p;
			if(amari[p]){
				loop = true;
				break;
			}
			else{
				amari[p] = true;
			}
			p *= 10;
		}
		for(int j = 0;j < i + 1;++j){
			cout << ans[j];
		}
		cout << endl;
		if(loop){
			bool hflag = false;
			for(int j = 0;j <= i;++j){
				if(hflag){
					cout << "^";
				}
				else{
					if(damari[j] == damari[i]){
						hflag = true;
					}
					cout << " ";
				}
			}
			cout << endl;
		}
		amari.clear();
	}
	return 0;
}