#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

const int IINF = INT_MAX;

bool converter(int value,int base,string &s){
  string ret = "";
  while( value >= base ){
    int tmp = value % base;
    value /= base;
    if( 0 <= tmp && tmp <= 9 ) ret += string(1,(char)('0'+tmp));
    else                       return false;
  }
  if( value != 0 ) {
    int tmp = value;
    if( 0 <= tmp && tmp <= 9 ) ret += string(1,(char)('0'+tmp));
    else                       return false;
  }
  if( ret.empty() ) ret = "0";
  reverse(ret.begin(),ret.end());
  s = ret;
  return true;
}

string itos(int i) { stringstream ss; ss << i; return ss.str(); }

int main(){
  int A,C;
  string B;
  while( cin >> A >> B >> C ){
    if( A == -1 && B == "-1" && C == -1 ) break;
    string boarder = B;
    bool failed = false;
    REP(age,A,C+1){
      //cout << age << " " << boarder << endl;
      int nebear = IINF;
      REP(base,2,17){
        string tmp = "";
        if( !converter(age,base,tmp) ) continue;
        if( age == A && tmp != boarder ) continue;
        else {
          int boar = (atoi)(boarder.c_str());
          int curr = (atoi)(tmp.c_str());
          if( boar > curr ) continue;
          nebear = min(nebear,curr);
        }
      }
      if( nebear == IINF ) failed = true;
      if( failed ) break;
      boarder = itos(nebear);
    }
    if( failed ) puts("-1");
    else cout << boarder << endl;
  }
  return 0;
}