public static void main ( String [ ] args ) throws IOException {
  BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in ) ) ;
  long n = Long . parseLong ( br . readLine ( ) ) ;
  long s = Long . parseLong ( br . readLine ( ) ) ;
  if ( n < s ) {
    System . out . println ( - 1 ) ;
    return ;
  }
  if ( n == s ) {
    System . out . println ( n + 1 ) ;
    return ;
  }
  long squareRootN = Double . valueOf ( Math . ceil ( Math . sqrt ( n ) ) ) . longValue ( ) ;
  for ( long base = 2 ;
  base <= squareRootN ;
  base ++ ) {
    long sum = sumDigitsInBase ( base , n ) ;
    if ( sum == s ) {
      System . out . println ( base ) ;
      return ;
    }
  }
  long nMinusS = n - s ;
  long minBase = - 1 ;
  for ( long p = 1 ;
  p <= squareRootN ;
  p ++ ) {
    if ( nMinusS % p != 0 ) {
      continue ;
    }
    long base = nMinusS / p + 1 ;
    if ( base > squareRootN ) {
      long sum = sumDigitsInBase ( base , n ) ;
      if ( sum == s ) {
        minBase = base ;
      }
    }
  }
  System . out . println ( minBase ) ;
}

public static void main ( String [ ] args ) throws IOException { BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; long n = Long . parseLong ( br . readLine ( ) ) ; long s = Long . parseLong ( br . readLine ( ) ) ; if ( n < s ) { System . out . println ( - 1 ) ; return ; } if ( n == s ) { System . out . println ( n + 1 ) ; return ; } long squareRootN = Double . valueOf ( Math . ceil ( Math . sqrt ( n ) ) ) . longValue ( ) ; for ( long base = 2 ; base <= squareRootN ; base ++ ) { long sum = sumDigitsInBase ( base , n ) ; if ( sum == s ) { System . out . println ( base ) ; return ; } } long nMinusS = n - s ; long minBase = - 1 ; for ( long p = 1 ; p <= squareRootN ; p ++ ) { if ( nMinusS % p != 0 ) { continue ; } long base = nMinusS / p + 1 ; if ( base > squareRootN ) { long sum = sumDigitsInBase ( base , n ) ; if ( sum == s ) { minBase = base ; } } } System . out . println ( minBase ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) (throws (type_identifier)) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (unary_expression operand: (decimal_integer_literal))))) (return_statement))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal))))) (return_statement))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))))) name: (identifier) arguments: (argument_list)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation name: (identifier) arguments: (argument_list (identifier) (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier)))) (return_statement))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (identifier)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (unary_expression operand: (decimal_integer_literal)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) consequence: (block (continue_statement))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal)))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation name: (identifier) arguments: (argument_list (identifier) (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (identifier))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))