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

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

int main() {
  int p;
  int q;
  int restart = 0;
  int stoped = 0;
  bool looped = false;
  
  scanf("%d %d", &p, &q);

  for (int i=0; 1; i++) {
    p *= 10;
    int shou = p / q;
    int amari = p % q;

    // printf("shou %d amari %d\n", shou, amari);
    


    rec_amari[i] = amari;
    rec_shou[i] = shou;
    for (int j=0; j<i; j++) {
      if (amari == rec_amari[j]) {
	restart = j;
	stoped = i;
	looped = true;

	// printf("stoped %d %d restart%d stoped%d", amari, shou, restart, stoped);
	goto END_LOOP;
      }
    }
    if (amari == 0) {
      stoped = i;
      break;
    }

    p = amari;
  }
 END_LOOP:
  // puts("");


  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("");
  }  
}