#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const double EPS = 1e-9;

int solve(int p, int q, stringstream &ss){
  map<int, int> m; for(int i = 0; m.find(p) == m.end(); ++i){
    if(p == 0) return -1;
    m[p] = i;
    p *= 10;
    ss <<p / q;
    p %= q;
  }
  return m[p];
}

int main() {
  int p, q;
  while(scanf("%d %d", &p, &q) != EOF){
    p %= q;
    stringstream ss;
    int d = solve(p, q, ss);
    cout <<ss.str() <<endl;
    if(d != -1){
      REP(i, ss.str().length()) cout <<(i < d ? " " : "^");
      cout <<endl;
    }
  }
  return 0;
}