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

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);

  int p, q;
  while(cin >> p >> q){
    set<int> amari;
    vector<int> seq;
    map<int, int > amarinoketa;
    int f = -1, s = -1;
    int keta = 0;
    while(1){
      seq.push_back(p/q);
      int am = p%q;
      if(am == 0) break;
      if(amarinoketa.find(am) != amarinoketa.end()){
	f = amarinoketa[am];
	s = keta;
	break;
      }else{
	amarinoketa[am] = keta;
      }
      p = (p%q) * 10;
      keta++;
    }
    if(f == s && f == -1){
      for(size_t i = 1; i < seq.size(); i++){
	cout << seq[i];
      }
      cout << endl;
    }else{
      for(size_t i = 1; i < seq.size(); i++){
	cout << seq[i];
      }
      cout << endl;
      for(size_t i = 0; i < seq.size()-1; i++){
	if(i >= (size_t)f){
	  cout << '^';
	}else{
	  cout << ' ';
	}
      }
      cout << endl;
    }
  }
  return 0;
}