#include <iostream>
#include <map>
#include <string>
#include <sstream>

using namespace std;

map< int, map<int, int> > cache;
int index;

string to_s(int n) {
    ostringstream os;
    os << n;
    return os.str();
}

string calc(int n, int d) {
    if (cache[n][d] != 0) {
        string ret;
        ret.push_back('\n');
        for (int i = 0; i < cache[n][d]; i++) ret.push_back(' ');
        for (int i = 0; i < index-cache[n][d]; i++) ret.push_back('^');
        ret.push_back('\n');
        return ret;
    }

    if (n % d == 0) return to_s(n/d) + "\n";
    cache[n][d] = index++;
    return to_s(n/d) + calc((n%d)*10, d);
}

int main() {
    int p, q;
    while (cin >> p >> q) {
        index = 0; 
        cache.clear();
        cout << calc(p*10, q);
    }
    return 0;
}