long findBase(in ulong n, in ulong sumDigits)
{
    import std.algorithm;
    // 1-digit
    if (n == sumDigits)
        return n + 1;
    foreach_reverse (digits; 2..n.binaryDigits)
    {
        immutable
            bmin = n.minb(digits).max(1 + sumDigits / digits),
            bmax = n.maxb(digits);
        if (n <= sumDigits + bmin ^^ (digits - 1) - 1)
            continue;
        if (digits * (bmax - 1) < sumDigits)
            continue;
        if (2 < digits)
        {
            foreach (base; bmin..bmax+1)
                if (base.f(n) == sumDigits)
                    return base;
            continue;
        }
        // two-digit: larger base, larger sumdigit.
        ulong left = bmin, right = bmax;
        while (1 < right - left)
        {
            immutable mid = (left + right) >> 1;
            if (mid.f(n) == sumDigits)
                if ((mid - 1).f(n) == sumDigits)
                    return mid - 1;
                else
                    return mid;
            else if (mid.f(n) < sumDigits)
                right = mid;
            else
                left = mid;
        }
        return (right.f(n) == sumDigits) ? right : -1;
    }
    return -1;
}

unittest
{
    import std.experimental.logger;trace;
    assert (87654.findBase(30) == 10);trace;
    assert (87654.findBase(138) == 100);trace;
    assert (87654.findBase(45678) == -1);trace;
    assert (31415926535.findBase(1) == 31415926535);trace;
    assert (1.findBase(31415926535) == -1);trace;
}

ulong f(in ulong base, in ulong n)
{
    if (n < base)
        return n;
    return (n % base) + base.f(n / base);
}

ulong binaryDigits(in ulong n)
{
    if (n < 3)
        return n;
    return (n >> 1).binaryDigits + 1;
}

unittest
{
    assert (0.binaryDigits == 0);
    assert (1.binaryDigits == 1);
    assert (2.binaryDigits == 2);
    assert (3.binaryDigits == 2);
    assert (4.binaryDigits == 3);
    assert (5.binaryDigits == 3);
    assert (6.binaryDigits == 3);
    assert (7.binaryDigits == 3);
    assert (8.binaryDigits == 4);
}

ulong minb(in ulong n, in ulong digits)
{
    return (n ^^ (real(1) / digits)).ffloor + 1;
}

ulong maxb(in ulong n, in ulong digits)
{
    return (n ^^ (real(1) / (digits - 1))).ffloor;
}

auto ffloor(real x)
{
    import std.math, std.conv;
    return (x + 0x1p-16).floor.to!ulong;
}

void main()
{
    import std.stdio, std.conv, std.string;
    readln.chomp.to!ulong.findBase(readln.chomp.to!ulong).writeln;
}
