#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <string>
#include <set>
#include <map>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int p,q;
  while(cin>>p>>q) {
    p %= q;
    string ans;
    set<int> se;
    map<int,int> ma;
    
    while(true) {
      if (p==0) {
        cout << ans << endl;
        break;
      }
      if (se.count(p)==1) {
        // cout << "p " << p << endl;
        cout << ans << endl;
        cout << string(ma[p],' ') << string(ans.length()-ma[p],'^') << endl;
        // cout << ma[p] << " " << ans.length() << endl;
        break;
      }
      se.insert(p);
      ma[p] = ans.length();
      
      p*=10;
      ans += '0' + p/q;
      p %= q;
    }
  }
}