require "prime"
N=gets.to_i
pd=N.prime_division
#warn pd.inspect
pd.map!{|a,b|a**b}
warn pd.inspect
min=(2*N)**2
def modinv(a,m)
	b=m
	u=1
	v=0
	while b>0
		t=a/b
		a-=t*b
		s=a
		a=b
		b=s
		u-=t*v
		s=u
		u=v
		v=s
	end
	u%=m
	if u<0
		u+=m
	end
	u
end
(2**pd.size).times{|i|
	k=1
	k1=1
	pd.size.times{|j|
		if i[j]==0
			k*=pd[j]
		else
			k1*=pd[j]
		end
	}
	warn [k,k1].inspect
	if k==1
		k=k1-1
	elsif k1==1
		k1=k+1
	else
		x=modinv(k1,k)
		k1*=x
		y=(k1-1)/k
		k=k1-1
		if x*y%2==1
			k*=2
		end
		#warn [x,k1,y,k].inspect
	end
	if k<min
		min=k
	end
}
puts min
