program main
  implicit none
  integer(8) :: n, s, sqrt_n, b, p
  read(*, *) n
  read(*, *) s

  if (n < s) then
    write(*, "(i0)") -1
    stop 0
  else if (n == s) then
    write(*, "(i0)") n + 1
    stop 0
  end if

  sqrt_n = integer_sqrt(n)

  do b = 2, sqrt_n
    if (f(b, n) == s) then
      write(*, "(i0)") b
      stop 0
    end if
  end do

  do p = sqrt_n, 1, -1
    b = (n - s) / p + 1
    if (b >= 2) then
      if (f(b, n) == s) then
        write(*, "(i0)") b
        stop 0
      end if
    end if
  end do

  write(*, "(i0)") -1

  contains

  function integer_sqrt(n)
    implicit none
    integer(8), intent(in) :: n
    integer(8) :: integer_sqrt, i
    if (n < 0) stop 101
    do i = 0, n + 1
      if (i ** 2 > n) then
        integer_sqrt = i - 1
        exit
      end if
    end do
  end function integer_sqrt

  recursive function f(b, n) result(res)
    implicit none
    integer(8), intent(in) :: b, n
    integer(8) :: res
    if (n < b) then
      res = n
    else
      res = f(b, n / b) + mod(n, b)
    end if
  end function f
end program main

; ModuleID = 'FIRModule'
source_filename = "FIRModule"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@_QQcl.e8171c46ef5b7d7165a31a6e263e7085 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_7937/source.f\00"
@_QQcl.28693029 = linkonce constant [4 x i8] c"(i0)"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

define void @_QQmain() local_unnamed_addr {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.e8171c46ef5b7d7165a31a6e263e7085, i32 4)
  %5 = call i1 @_FortranAioInputInteger(ptr %4, ptr nonnull %2, i32 8)
  %6 = call i32 @_FortranAioEndIoStatement(ptr %4)
  %7 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.e8171c46ef5b7d7165a31a6e263e7085, i32 5)
  %8 = call i1 @_FortranAioInputInteger(ptr %7, ptr nonnull %3, i32 8)
  %9 = call i32 @_FortranAioEndIoStatement(ptr %7)
  %10 = load i64, ptr %2, align 8, !tbaa !3
  %11 = load i64, ptr %3, align 8, !tbaa !3
  %12 = icmp slt i64 %10, %11
  br i1 %12, label %13, label %18

13:                                               ; preds = %0
  %14 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.e8171c46ef5b7d7165a31a6e263e7085, i32 8)
  %15 = call i1 @_FortranAioOutputInteger32(ptr %14, i32 -1)
  %16 = call i32 @_FortranAioEndIoStatement(ptr %14)
  %17 = call {} @_FortranAStopStatement(i32 0, i1 false, i1 false)
  unreachable

18:                                               ; preds = %0
  %19 = icmp eq i64 %10, %11
  br i1 %19, label %20, label %27

20:                                               ; preds = %18
  %21 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.e8171c46ef5b7d7165a31a6e263e7085, i32 11)
  %22 = load i64, ptr %2, align 8, !tbaa !3
  %23 = add i64 %22, 1
  %24 = call i1 @_FortranAioOutputInteger64(ptr %21, i64 %23)
  %25 = call i32 @_FortranAioEndIoStatement(ptr %21)
  %26 = call {} @_FortranAStopStatement(i32 0, i1 false, i1 false)
  unreachable

27:                                               ; preds = %18
  %28 = icmp slt i64 %10, 0
  br i1 %28, label %29, label %31

29:                                               ; preds = %27
  %30 = call {} @_FortranAStopStatement(i32 101, i1 false, i1 false)
  unreachable

31:                                               ; preds = %27
  %32 = add nuw i64 %10, 2
  %33 = icmp sgt i64 %32, 0
  br i1 %33, label %.lr.ph.i, label %._crit_edge

.lr.ph.i:                                         ; preds = %31, %36
  %.0611.i = phi i64 [ %37, %36 ], [ 0, %31 ]
  %34 = mul i64 %.0611.i, %.0611.i
  %35 = icmp sgt i64 %34, %10
  br i1 %35, label %_QFPinteger_sqrt.exit, label %36

36:                                               ; preds = %.lr.ph.i
  %37 = add nuw nsw i64 %.0611.i, 1
  %exitcond.not.i = icmp eq i64 %37, %32
  br i1 %exitcond.not.i, label %._crit_edge, label %.lr.ph.i

_QFPinteger_sqrt.exit:                            ; preds = %.lr.ph.i
  %38 = add nsw i64 %.0611.i, -1
  store i64 2, ptr %1, align 8, !tbaa !3
  %39 = icmp ugt i64 %.0611.i, 2
  br i1 %39, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %49, %_QFPinteger_sqrt.exit
  %40 = icmp ugt i64 %.0611.i, 1
  br i1 %40, label %.lr.ph19, label %._crit_edge

.lr.ph19:                                         ; preds = %.preheader
  %41 = sub i64 %10, %11
  br label %51

.lr.ph:                                           ; preds = %_QFPinteger_sqrt.exit, %49
  %storemerge15 = phi i64 [ %50, %49 ], [ 2, %_QFPinteger_sqrt.exit ]
  %42 = call i64 @_QFPf(ptr nonnull %1, ptr nonnull %2)
  %43 = icmp eq i64 %42, %11
  br i1 %43, label %44, label %49

44:                                               ; preds = %.lr.ph
  %45 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.e8171c46ef5b7d7165a31a6e263e7085, i32 19)
  %46 = call i1 @_FortranAioOutputInteger64(ptr %45, i64 %storemerge15)
  %47 = call i32 @_FortranAioEndIoStatement(ptr %45)
  %48 = call {} @_FortranAStopStatement(i32 0, i1 false, i1 false)
  unreachable

49:                                               ; preds = %.lr.ph
  %50 = add nuw i64 %storemerge15, 1
  store i64 %50, ptr %1, align 8, !tbaa !3
  %exitcond.not = icmp eq i64 %storemerge15, %38
  br i1 %exitcond.not, label %.preheader, label %.lr.ph

51:                                               ; preds = %.lr.ph19, %63
  %.018 = phi i64 [ %38, %.lr.ph19 ], [ %64, %63 ]
  %52 = sdiv i64 %41, %.018
  %53 = add i64 %52, 1
  store i64 %53, ptr %1, align 8, !tbaa !3
  %54 = icmp sgt i64 %53, 1
  br i1 %54, label %55, label %63

55:                                               ; preds = %51
  %56 = call i64 @_QFPf(ptr nonnull %1, ptr nonnull %2)
  %57 = icmp eq i64 %56, %11
  br i1 %57, label %58, label %63

58:                                               ; preds = %55
  %59 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.e8171c46ef5b7d7165a31a6e263e7085, i32 28)
  %60 = call i1 @_FortranAioOutputInteger64(ptr %59, i64 %53)
  %61 = call i32 @_FortranAioEndIoStatement(ptr %59)
  %62 = call {} @_FortranAStopStatement(i32 0, i1 false, i1 false)
  unreachable

63:                                               ; preds = %55, %51
  %64 = add nsw i64 %.018, -1
  %65 = icmp sgt i64 %.018, 1
  br i1 %65, label %51, label %._crit_edge

._crit_edge:                                      ; preds = %36, %63, %31, %.preheader
  %66 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.e8171c46ef5b7d7165a31a6e263e7085, i32 34)
  %67 = call i1 @_FortranAioOutputInteger32(ptr %66, i32 -1)
  %68 = call i32 @_FortranAioEndIoStatement(ptr %66)
  ret void
}

define i64 @_QFPinteger_sqrt(ptr nocapture readonly %0) local_unnamed_addr {
  %2 = load i64, ptr %0, align 8, !tbaa !3
  %3 = icmp slt i64 %2, 0
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  %5 = tail call {} @_FortranAStopStatement(i32 101, i1 false, i1 false)
  unreachable

6:                                                ; preds = %1
  %7 = add nuw i64 %2, 2
  %8 = icmp sgt i64 %7, 0
  br i1 %8, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %6, %13
  %.0611 = phi i64 [ %14, %13 ], [ 0, %6 ]
  %9 = mul i64 %.0611, %.0611
  %10 = icmp sgt i64 %9, %2
  br i1 %10, label %11, label %13

11:                                               ; preds = %.lr.ph
  %12 = add nsw i64 %.0611, -1
  br label %.loopexit

13:                                               ; preds = %.lr.ph
  %14 = add nuw nsw i64 %.0611, 1
  %exitcond.not = icmp eq i64 %14, %7
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph

.loopexit:                                        ; preds = %13, %6, %11
  %.0 = phi i64 [ %12, %11 ], [ undef, %6 ], [ undef, %13 ]
  ret i64 %.0
}

; Function Attrs: nofree nosync nounwind memory(argmem: read)
define i64 @_QFPf(ptr nocapture readonly %0, ptr nocapture readonly %1) local_unnamed_addr #0 {
  %3 = alloca i64, align 8
  %4 = load i64, ptr %1, align 8, !tbaa !3
  %5 = load i64, ptr %0, align 8, !tbaa !3
  %6 = icmp slt i64 %4, %5
  br i1 %6, label %12, label %7

7:                                                ; preds = %2
  %8 = sdiv i64 %4, %5
  store i64 %8, ptr %3, align 8, !tbaa !3
  %9 = call i64 @_QFPf(ptr nonnull %0, ptr nonnull %3)
  %10 = srem i64 %4, %5
  %11 = add i64 %10, %9
  br label %12

12:                                               ; preds = %2, %7
  %.0 = phi i64 [ %11, %7 ], [ %4, %2 ]
  ret i64 %.0
}

declare ptr @_FortranAioBeginExternalListInput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioInputInteger(ptr, ptr, i32) local_unnamed_addr

declare i32 @_FortranAioEndIoStatement(ptr) local_unnamed_addr

declare ptr @_FortranAioBeginExternalFormattedOutput(ptr, i64, ptr, i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

declare {} @_FortranAStopStatement(i32, i1, i1) local_unnamed_addr

declare i1 @_FortranAioOutputInteger64(ptr, i64) local_unnamed_addr

attributes #0 = { nofree nosync nounwind memory(argmem: read) }

!llvm.module.flags = !{!0, !1, !2}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{!4, !4, i64 0}
!4 = !{!"any data access", !5, i64 0}
!5 = !{!"any access", !6, i64 0}
!6 = !{!"Flang Type TBAA Root"}
