#include <iostream>
#include <string>
#include <vector>
using namespace std;

int check( vector< int >& vec, int p )
{
	for( int i = 0; i < (int)vec.size(); ++i ){
		if( p == vec[ i ] ){
			return i;
		}
	}
	return -1;
}

int main()
{
	int p, q;
	while( cin >> p >> q ){
		string answer;
		vector< int > covered;
		int loop = -1;

		while( p != 0 ){
			while( 1 ){
				p *= 10;
				loop = check( covered, p );
				if( loop != -1 ){
					break;
				}
				covered.push_back( p );
				if( p >= q ){
					break;
				}
				answer += '0';
			}
			if( loop != -1 ) break;

			int div = p / q;
			answer += (char)( div + '0' );
			p %= q;
		}

		cout << answer << endl;
		if( loop != -1 ){
			for( int i = 0; i < loop; ++i ){
				cout << ' ';
			}
			for( int i = loop; i < (int)answer.size(); ++i ){
				cout << '^';
			}
			cout << endl;
		}
	}
	return 0;
}