import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;

Tuple!(string,int,int) div(int p,int q){
    Tuple!(int,int)[] list;
    string s1,s2;
    string ret = "";
    s1 = to!string(p);
    s2 = to!string(q);
    int ind,n;
    bool flg = false;
    n = to!int(s1[ind] - '0');
    ind++;
    n *= 10;
    while(n){
	s1 ~= '0';
	n += to!int(s1[ind] - '0');
	ind++;
	int a = n % q;
	int b = to!int(n / q);
	for(int i=0;i<list.length;i++){
	    if(a == list[i][0] && b == list[i][1]){
	       	flg = true;
		return tuple(ret,i,ind-1);
	    }
	}
	ret ~= to!string(b);
	list ~= tuple(a,b);
	n = a;
	n *= 10;
    }
    return tuple(ret,-1,-1);
}

void main(){

    while(true){
	string s1 = readln();
	if(stdin.eof()) break;
	auto s = s1.split().to!(int[]);
	int p = s[0];
	int q = s[1];
	auto ans = div(p,q);
	writeln(ans[0][to!string(p).length-1..$]);
	if(ans[1] != -1){
	    string ret="";
	    for(int i=0;i<ans[0].length;i++){
		if(ans[1]-1 <= i &&  i < ans[2]-2) {
		    ret ~= "^";
		}else ret ~= " ";
	    }
	    writeln(ret);
	}
    }

}