#include <iostream>
#include <cstdio>
using namespace std;

int rec_amari[100];
int rec_shou[100];

int main() {
  int p;
  int q;

  while(scanf("%d %d", &p, &q) != EOF) {
    int restart = 0;
    int stoped = 0;
    bool looped = false;
    
    for (int i=0; 1; i++) {
      p *= 10;
      int shou = p / q;
      int amari = p % q;
      
      rec_amari[i] = amari;
      rec_shou[i] = shou;

      // 
      if (amari == 1) {
	  restart = -1;
	  stoped = i;
	  looped = true;
	  goto END_LOOP;
      }
      
      // ???????????????
      for (int j=0; j<i; j++) {
	if (amari == rec_amari[j]) {
	  restart = j;
	  stoped = i;
	  looped = true;
	  goto END_LOOP;
	}
      }

      // ???????????????
      if (amari == 0) {
	stoped = i;
	break;
      }
      
      p = amari;
    }
  END_LOOP:

    // ????????????
    for (int i=0; i<=stoped; i++) {
      printf("%d", rec_shou[i]);
    }
    puts("");
    // ????????????????????????
    if (looped) {
      for (int i=0; i<=stoped; i++) {
	if (i > restart) {
	  printf("^");
	} else {
	  printf(" ");
	}
      }
      puts("");
    }
    
  }
}