#include <bits/stdc++.h>
using namespace std;
int main() {
  map<char, int> ma;
  for (int i = '0'; i <= '9'; i++) ma[i] = i - '0';
  for (int i = 'A'; i <= 'Z'; i++) ma[i] = i - 'A' + 10;
  char s1[10], s2[10];
  scanf("%[^:]", s1);
  getchar();
  scanf("%s", s2);
  int len1 = strlen(s1);
  int len2 = strlen(s2);
  int maxx = 0;
  for (int i = 0; i < len1; i++) maxx = max(maxx, ma[s1[i]]);
  for (int i = 0; i < len2; i++) maxx = max(maxx, ma[s2[i]]);
  int ok1 = 0, ok2 = 0;
  for (int i = maxx + 1; i < 65; i++) {
    long long t1 = 0;
    for (int j = len1 - 1, k = 0; j >= 0; j--, k++) {
      t1 += (long long)(pow(i, k) + 0.00000001) * ma[s1[j]];
      if (t1 >= 24) {
        ok1 = i;
        break;
      }
    }
    long long t2 = 0;
    for (int j = len2 - 1, k = 0; j >= 0; j--, k++) {
      t2 += (long long)(pow(i, k) + 0.00000001) * ma[s2[j]];
      if (t2 >= 60) {
        ok2 = i;
        break;
      }
    }
    if (ok1 || ok2) break;
  }
  if (ok1 == 0) ok1 = 0x7fffffff;
  if (ok2 == 0) ok2 = 0x7fffffff;
  int temp = min(ok1, ok2);
  if (temp == 0x7fffffff)
    puts("-1");
  else if (temp <= maxx + 1)
    puts("0");
  else {
    for (int i = maxx + 1; i < temp; i++) {
      printf("%d", i);
      if (i != temp - 1) printf(" ");
    }
  }
  return 0;
}
