public void solve() throws IOException {
    String nextline;
    while (!(nextline = nextLine()).equals("end")) {
      String[] line = nextline.split(" ");
      String[] comm = line[0].split("-");

      String number = line[1];
      String command = comm[0];
      int radix = -1 * Integer.parseInt(comm[1]);

      if (command.equals("to")) {
        int num = Integer.parseInt(number);

        StringBuilder ans = new StringBuilder();
        do {
          int mod = num % radix;
          num /= radix;
          if (mod < 0) {
            mod += Math.abs(radix);
            num++;
          }
          ans.insert(0, mod);
        } while (Math.abs(num) > 0);
        System.out.println(ans);
      } else if (command.equals("from")) {
        int total = 0;
        int base = 1;
        for (int x = number.length() - 1; x >= 0; x--) {
          total += (number.charAt(x) - 48) * base;
          base *= radix;
        }
        System.out.println(total);
      }
    }
  }