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 -1;
    if(n>=(n-s+1)*2) return -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;
    }
    return n-s+1;
}

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