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

int main() {
    int p, q;
    while (cin >> p >> q) {
        map<int,int> pos;
        stringstream ss;
        int l, r = -1;
        for (int i=0; ; ++i) {
            if (p % q == 0) {
                break;
            }
            if (pos.count(p % q)) {
                l = pos[p % q];
                r = i-1;
                break;
            }
            pos[p % q] = i;
            p = (p % q) * 10;
            ss << p / q;
        }
        string ans = ss.str();

        cout << ans << endl;
        if (r < 0) continue;

        for (int i=0; i<=r; ++i) {
            if (l <= i) {
                cout << "^";
            } else {
                cout << " ";
            }
        }
        cout << endl;
    }
    return 0;
}