#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

void solve(int p, int q) {
  p *= 10;
  int pos = 0;
  map<int, int>vis;
  while(p != 0) {
    int k;
    for(k = 1; k <= 9 && q*k <= p; ++k);
    --k;
    cout << k;

    p -= q*k;
    if(vis.find(p) != vis.end()) {
      cout << endl;
      for(int i = 0; i <= pos; ++i) {
	if(i <= vis[p]) cout << ' ';
	else           cout << '^';
      }
      break;
    }
    vis[p] = pos;
    p *= 10;
    ++pos;
  }
  cout << endl;
}

main() {
  int p, q;
  while(cin >> p >> q) solve(p, q);
}