#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>

using namespace std;

int main(){
	
	int p,q;
	while(cin>>p>>q){
		set<int> dNumList;
		string retStr;
		int pp=p;
		int qq=q;
		dNumList.insert(pp);
		bool f=false;


		bool z=false;
		// pp>qqツづ個湘ェツ債、pp<qqツづ可づ按づゥツづ慊づ可可算
		while(1){
			if(pp<qq){
				int cnt=0;
				while(pp<qq){
					pp*=10;
					if(dNumList.find(pp)!=dNumList.end()){
						f=true;
						break;
					}
					if(cnt>=1)
						retStr+='0';
					dNumList.insert(pp);
					cnt++;
				}
				if(f)
					break;
			}

			int left=pp%qq;
			pp/=qq;
			retStr+=(char)(pp+'0');
			pp=left;

			if(dNumList.find(pp)!=dNumList.end())
				break;
			if(pp==0){
				z=true;
				break;
			}
			dNumList.insert(pp);
		}
		f=false;
		string sufs;
		if(z){
			cout<<retStr<<endl;
			continue;
		}

		if(pp!=0){
			dNumList.clear();
			dNumList.insert(pp);

			while(1){
				if(pp<qq){
					int cnt=0;
					while(pp<qq){
						pp*=10;
						if(dNumList.find(pp)!=dNumList.end()){
							f=true;
							break;
						}
						if(cnt>=1)
							sufs+='0';
						dNumList.insert(pp);
						cnt++;
					}
					if(f)
						break;
				}
				
				int left=pp%qq;
				pp/=qq;
				sufs+=(char)(pp+'0');
				pp=left;
				
				if(dNumList.find(pp)!=dNumList.end()||pp==0)
					break;
				dNumList.insert(pp);
			}
		}			

		cout<<retStr<<endl;
		int empSize=retStr.size()-sufs.size();
		string loops;
		for(int i = 0; i < retStr.size(); i++){
			if(i>=empSize){
				loops+='^';
			}
			else{
				loops+=' ';
			}
		}
			cout<<loops<<endl;
	}

	return 0;
}