macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        n: usize,
        s: usize
    }
    if s > n {
        println!("-1");
        return;
    }
    if s == n {
        println!("{}", n + 1);
        return;
    }
    let mut sq = 1;
    while sq * sq <= n {
        sq += 1;
    }
    sq -= 1;
    for i in 2..sq + 1 {
        let mut dig = 0;
        let mut m = n;
        while m > 0 {
            dig += m % i;
            m /= i;
        }
        if dig == s {
            println!("{}", i);
            return;
        }
    }
    let mut divs = divisors(n - s);
    divs.sort_by(|x, y| y.cmp(x));
    if sq * sq < n {
        sq += 1;
    }
    for i in divs.iter().skip_while(|&&x| x >= sq || x > s) {
        let b = (n - s) / i + 1;
        if s - i == n % b {
            println!("{}", b);
            return;
        }
    }
    println!("-1");
}

fn factor(mut n: usize) -> Vec<(usize, usize)> {
    let mut ret = vec![];
    let mut p = 0;
    while n % 2 == 0 {
        p += 1;
        n /= 2;
    }
    if p != 0 {
        ret.push((2, p));
    }
    let mut r = 3;
    while r * r <= n {
        p = 0;
        while n % r == 0 {
            p += 1;
            n /= r;
        }
        if p != 0 {
            ret.push((r, p));
        }
        r += 2;
    }
    if n != 1 {
        ret.push((n, 1));
    }
    ret
}

fn divisors(n: usize) -> Vec<usize> {
    if n == 1 {
        return vec![1];
    }
    let mut ret = vec![];
    let fac = factor(n);
    let m = fac.len();
    let mut st = vec![0; m];
    while st[m - 1] <= fac[m - 1].1 {
        let mut d = 1;
        for i in 0..m {
            d *= fac[i].0.pow(st[i] as u32);
        }
        ret.push(d);
        st[0] += 1;
        let mut j = 0;
        while j < m - 1 && st[j] > fac[j].1 {
            st[j] = 0;
            st[j + 1] += 1;
            j += 1;
        }
    }
    ret
}


Measured memory_bytes: 4352