#include<bits/stdc++.h>
using namespace std;

int main(){
	map<int,int> latte;
	int P,Q;
	while(cin>>P>>Q){
		int m=0,l=0,r=0;
		vector<int> ans;
		m = P%Q;
		while(m){
			int nm = m*10%Q;
			if(latte.find(nm)!=latte.end()){
				l = latte[nm]; r = ans.size(); break;
			}
			ans.push_back(m*10/Q);
			latte[m] = ans.size()-1;
			m = nm;
		}
		for(int i=0;i<ans.size();i++) printf("%d",ans[i]); puts("");
		if(r!=0){
			for(int i=0;i<ans.size();i++){
				if(l<=i) printf("^");
				else printf(" ");
			}puts("");
		}
	}
	return 0;
}