import std.stdio;
import std.string;
import std.conv;
import std.array;
import std.algorithm;

void main(){
	string s;
	while(stdin.readln(s) != EOF){
		int a, b;
		string[] ss = s.split();
		a = ss[0].to!int() * 10;
		b = ss[1].to!int();
		int begin, end = -1;
		int[] qs, rs;
		while(a){
			qs ~= a/b;
			if(!find(rs, a%b).empty){
				begin = cast(int)(qs.length - find(rs, a%b).length);
				end = cast(int)rs.length;
				rs ~= a%b;
				break;
			}
			rs ~= a%b;
			a = a%b*10;
		}
		
		foreach(q; qs){
			write(q);
		}
		writeln("");
		
		if(end<0)continue;
		for(int i=0;i<qs.length;i++){
			if(i >= begin && i <= end)
				write("^");
			else
				write(" ");
		}
		writeln("");
	}
}