#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

int main(int argc, char *argv[]) {
  int p, q, point;
  bool loop;
  vector<int> hist;
  ostringstream os;
  while (cin >> p >> q) {
    p *= 10;
    point = 0;
    loop = false;
    hist.clear();

    while (p != 0) {
      while (p < q) {
        hist.push_back(p);
        p *= 10;
        os << 0;
      }
      for (int i = 0; i < hist.size(); i++)
        if (hist[i] == p) {
          loop = true;
          point += i;
          break;
        }
      if (loop)
        break;

      hist.push_back(p);
      os << p / q;
      p = p % q * 10;

      for (int i = 0; i < hist.size(); i++)
        if (hist[i] == p) {
          loop = true;
          point += i;
          break;
        }
      if (loop)
        break;
    }

    cout << os.str() << endl;
    if (loop) {
      for (int i = 0; i < point; i++)
        cout << ' ';
      for (int i = point; i < os.str().size(); i++)
        cout << '^';
      cout << endl;
    }
    os.str("");
    os.clear();
  }
  return 0;
}