#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 long long ll;
ll p,q;
bool ch;
bool flag;
string result;
string result2;
map<ll,bool> index;
int debug; 

ll dfs(ll value)
{
  debug++;
  assert(debug <= 85);
  if(!value)
    {
      ch = true;
      flag = false;
      return -(1<<29);
    }
  // cout<< "value = " << value << " : " << result << endl;
  assert(value >= 0);
  if(index[value])return value;
  index[value] = true;
  if(value < q)
    {
      result+="0";
      ll res = dfs(value*10);
      if(flag)result2 += "^";
      else    result2 += " ";
      if(res == value)flag = false;
      return res;
    }
  else
    { 
      ll v = value/q;
      if(v >= 9)
    {
      v = 9;
       
    }
      result += (char)('0'+v);
      ll res = dfs(10*(value-q*v));
      if(flag)result2 += "^";
      else    result2 += " ";
      if(res == value)flag = false;
      return res;
    }
}
 
int main()
{
 
  while(cin >> p >> q)
    {
      ch = false;
      debug = 0;
      result2.clear();
      result.clear();
      index.clear();
      flag = true;
      dfs(p);
      reverse(result2.begin(),result2.end());
      cout << result.substr(1) << endl;
      if(!ch)cout << result2.substr(1) << endl;
    }
  return 0;
}