#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int main(){
	
	int p, q, loop_start, loop_end;
	
	int a[20000], rest[20000];
	
	bool checker;
	
	cin >> p;
	
	while(true){
		
		if(cin.eof()){
			break;
		}
		
		cin >> q;
		
		//cout << p / q << ".";
		
		p = p % q;
		
		if(p == 0){
			printf("0\n");
			cin >> p;
			continue;
		}
		
		rest[0] = p;
		
		loop_start = 1;
		loop_end = 99;
		
		for(int i = 0; i < 100; i++){
			
			checker = false;
			
			p *= 10;
			
			for(int j = 1; j <= 10; j++){
				if(p < j * q){
					p -= (j - 1) * q;
					if(p == 0){
						checker = true;
						loop_start = -1;
						loop_end = i + 1;
					}
					a[i] = j - 1;
					break;
				}
			}
			
			rest[i + 1] = p;
			
			for(int j = 0; j <= i; j++){
				if(rest[j] == p){
					checker = true;
					loop_start = j;
					loop_end = i + 1;
					break;
				}
			}
			
			if(checker){
				break;
			}
			
		}
		
		for(int i = 0; i < loop_end; i++){
			cout << a[i];
		}
		
		cout << endl;
		
		if(loop_start >= 0){
			for(int i = 0; i < loop_end; i++){
				if(i >= loop_start){
					cout << '^';
				}else{
					cout << ' ';
				}
			}
			cout << endl;
		}
		
		
		cin >> p;
	}
	
	return 0;
}