import std.stdio, std.string, std.conv;
import std.math;
 
auto solve(string s_)
{
    immutable n = s_.to!long();
    immutable s = readln.chomp.to!long();
    if(s==n) return s+1;
    immutable m = sqrt(n.to!real()).to!long+1;
    foreach(i;2..m)
    {
        long v=n, c=0;
        while(v) c+=v%i, v/=i;
        if(c==s) return i;
    }
    foreach_reverse(i;1..m)
    {
        auto v1=n/i, v2=n/(i+1), r=s-i-(n%(n/i)), v=v1-r/i;
        if(r>=0 && r%i==0 && v2<v && v<=v1 && v>i)
            return v;
    }
    return -1;
}

void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }