#include<iostream>
#include<vector>
using namespace std;

int main(){
  int p,q;
  int tmp;
  int table[1000000];
  vector<int> ans;

  while(cin >> p >> q){
    ans.clear();
    for(int i=0;i<q;i++)table[i] = 0;
    p = p%q;
    table[p] = 0;
    p *= 10;
    while(1){
      tmp = p/q;
      ans.push_back(tmp);
      p = p%q;
      if(!p){
	tmp = -1;
	break;
      }else if(table[p]){
	tmp = table[p];
	break;
      }else{
	table[p] = ans.size();
	p *= 10;
      }
    }

    if(tmp<0){
      for(int i=0;i<ans.size();i++)cout << ans[i];
      cout << endl;
    }else{
      for(int i=0;i<ans.size();i++)cout << ans[i];
      cout << endl;
      for(int i=0;i<tmp;i++)cout << " ";
      for(int i=tmp;i<ans.size();i++)cout << '^';
      cout << endl;
    }
  }
}