#include "bits/stdc++.h"

using namespace std;

typedef pair<int,int> pii;
typedef vector<int> vi;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999


int fin(vector<pii> vec,int x){
    rep(i,vec.size()){
        if(vec[i].first==x)return vec[i].second;
    }
    return -1;
}

int main(){
    int a,b;
    while(cin>>a>>b){
        static vector<pii> used[1000001];
        rep(i,1000001)used[i].clear();
        string ans="";
        int c=0;
        a*=10;
        while(1){
            int x=a/b;
            int y=a%b;
//            cout<<x<<" "<<y<<endl;
            if(y==0){ans+='0'+x;cout<<ans<<endl;break;}
            int res=-1;
            if( used[y].size() && (res = fin(used[y],x))!=-1 ){
                cout<<ans<<endl;
                rep(i,res)cout<<" ";
                rep(i,ans.size()-res)cout<<'^';
                cout<<endl;
                break;
            }
            ans+='0'+x;
            used[y].pb(pii(x,c));
            a-=b*x;
            c++;
            a*=10;
        }
    }
    
}