#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cassert>
#include<map>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;
map<int,bool> EX;
int p,q;
bool found;
int debug;

int dfs(int v,int &cnt)
{
  if(v == 0)return v;
  if(EX[v])return v;
  EX[v] = true;
  
  int va = v/q;
  if(va >= 9)va = 9;
  cout << va;
  int res = dfs((v-q*va)*10,cnt);
  debug++;
  if(found)return res;
  if(v == res)found = true;
  cnt++;  
  return res;
}

int main()
{
   while(cin >> p >> q)
    {
      debug = 0;
      found = false;
      EX.clear();
      int cnt = 0;
      int res = dfs(p*10,cnt);
      cout << endl;
      if(!res)continue;
      rep(i,debug-cnt)cout << ' ';
      rep(i,cnt)      cout << '^';
      cout << endl;
    }
  return 0;
}