#include<bits/stdc++.h>
using namespace std;

int p,q;

int main(){
  while( cin >> p >> q ){
    map<int,int> M;
    string res="";
    int pos = -1;
    int cnt = 0;
    while( p != 0 ){
      p*=10;
      res += ('0'+p/q);
      p%=q;
      if( M.count( p ) != 0 ) {
        pos = M[p];
        break;      
      }
      M[p] = cnt++;
    }
    cout << res << endl;
    if( pos != -1 ){
      for(int i=0;i<(int)res.size();i++){
        if( i > pos ) cout << "^";
        else cout << " ";
      }
      cout << endl;
    }
  }
}