#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

const int INF = INT_MAX / 2;

int change(int a, int m)
{
    int tmp = 1;
    int ret = 0;
    while(a > 0){
        if(a % m >= 10)
            return INF;
        ret += (a % m) * tmp;
        a /= m;
        tmp *= 10;
    }
    return ret;
}

int main()
{
    for(;;){
        int a, b, c;
        cin >> a >> b >> c;
        if(a == -1)
            return 0;

        bool ng = true;
        for(int m=2; m<=16; ++m){
            if(change(a, m) == b)
                ng = false;
        }
        if(ng){
            cout << -1 << endl;
            continue;
        }

        while(++a <= c){
            int b2 = INF;
            for(int m=2; m<=16; ++m){
                int tmp = change(a, m);
                if(b <= tmp && tmp < b2)
                    b2 = tmp;
            }
            if(b2 == INF){
                b = -1;
                break;
            }
            b = b2;
        }

        cout << b << endl;
    }
}