#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

int main() {
    int p, q;
    while ( cin >> p >> q ) {
        map <int, int> M;
        int t = 0;
        int r = p % q;
        while ( r > 0 ) {
            r *= 10;
            if ( M.count( r ) ) {
                cout << endl;
                for ( int i = 0; i < t; ++ i ) {
                    if ( i >= M[r] && i <= t - 1 ) {
                        cout << "^";
                    } else {
                        cout << " ";
                    }
                }
                break;
            }
            M[r] = t;
            cout << r / q;
            r %= q;
            t ++;
        }
        cout << endl;
    }
    return 0;
}