Let's consider equation:x^2 + s(x)·x - n = 0,  

where x, n are positive integers, s(x) is the function, equal to the sum of digits of number x in the decimal number system.

You are given an integer n, find the smallest positive integer root of equation x, or else determine that there are no such roots.


-----Input-----

A single line contains integer n (1 ≤ n ≤ 10^18) — the equation parameter.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. 


-----Output-----

Print -1, if the equation doesn't have integer positive roots. Otherwise print such smallest integer x (x > 0), that the equation given in the statement holds.


-----Examples-----
Input
2

Output
1

Input
110

Output
10

Input
4

Output
-1



-----Note-----

In the first test case x = 1 is the minimum root. As s(1) = 1 and 1^2 + 1·1 - 2 = 0.

In the second test case x = 10 is the minimum root. As s(10) = 1 + 0 = 1 and 10^2 + 1·10 - 110 = 0.

In the third test case the equation has no roots.

Source: codeforces | Difficulty: MEDIUM
Tags: Sorting

Mathematics

Complete search


Solutions:
from math import sqrt

def s(x):
	x = int(x)
	x = abs(x)
	x = str(x)
	su = 0
	for i in range(len(x)):
		su += int(x[i])
	return su
a = int(input())
for i in range(1, 91):
	f = (-i + sqrt(i ** 2 + 4 * a)) / 2
	if f == int(f) and s(f) == i and (int(f) > 0) and (int(f) ** 2 + s(f) * int(f) - a == 0):
		print(int(f))
		break
else:
	print(-1)


from sys import stdin, stdout
from os import path
rd = lambda : stdin.readline().strip()
wr = stdout.write
if path.exists('input.txt'):
	stdin = open('input.txt', 'r')
import time, math

def sumDigits(x):
	return sum(map(int, str(x)))
x = int(rd())
for i in range(max(1, int(x ** (1 / 2)) - 162), int(x ** (1 / 2)) + 1):
	if i * (i + sumDigits(i)) == x:
		print(i)
		exit()
print(-1)


import math

def digit_sum(n):
	su = 0
	while n:
		su += n % 10
		n = math.floor(n / 10)
	return su

def main():
	n = int(input())
	mini = -1
	for s in range(1, 9 * 10 + 1):
		a = (-s + math.sqrt(s * s + 4 * n)) / 2
		b = (-s - math.sqrt(s * s + 4 * n)) / 2
		x = math.floor(a)
		y = math.floor(b)
		if a.is_integer() and x != 999959999 and (x > 0) and (digit_sum(x) == s) and (mini == -1 or x < mini):
			mini = x
		if b.is_integer() and x != 999959999 and (y > 0) and (digit_sum(y) == s) and (mini == -1 or y < mini):
			mini = y
	print(mini)

def __starting_point():
	main()
__starting_point()


n = int(input())
x = int(n ** 0.5)
if x >= 10:
	d = int(len(str(x)))
	d = d * 9
else:
	d = x
s = 1
while s <= d:
	y = -s + (s * s + 4 * n) ** 0.5
	x = int(y / 2)
	if x * x + s * x == n:
		break
	s += 1
if s > d:
	x = -1
print(x)


n = int(input())
f = lambda x: x * x + sum(map(int, str(abs(x)))) * x

def b(l, r):
	while l < r - 1:
		m = (l + r) // 2
		if f(m) < n:
			l = m
		else:
			r = m
	return r
r = b(0, 10 ** 18)
for i in range(max(1, r - 1000), r + 1000):
	if f(i) == n:
		r = i
		break
else:
	r = -1
print(r)


n = int(input())
k = int(n ** (1 / 2))
for i in range(max(0, k - 162), k + 162):
	if i * i + sum(map(int, str(i))) * i - n == 0:
		print(i)
		break
else:
	print(-1)


def f(n):
	return sum(list(map(int, list(str(n)))))
n = int(input())
for i in range(1, 100):
	d = int((i ** 2 + 4 * n) ** 0.5)
	a = 1
	b = i
	a1 = (-b + d) // (2 * a)
	a2 = (-b - d) // (2 * a)
	if a1 > 0 and a1 ** 2 + a1 * f(a1) - n == 0:
		print(a1)
		exit(0)
	elif a2 > 0 and a2 ** 2 + a2 * f(a2) - n == 0:
		print(a2)
		exit(0)
print(-1)


def readln():
	return tuple(map(int, input().split()))
(n,) = readln()
for s in range(1, 180):
	d = int((s ** 2 + 4 * n) ** 0.5)
	if d ** 2 == s ** 2 + 4 * n and (-s + d) % 2 == 0:
		x = (-s + d) // 2
		if sum(map(int, list(str(x)))) == s:
			import sys
			print(x)
			return
print(-1)


n = int(input())
x = int(round(n ** 0.5 - 0.499999))
y = x - 9999
while x >= y and x > 0:
	list_1 = list(map(int, str(x)))
	s = 0
	for i in list_1:
		s = s + i
	if x * x + s * x - n == 0:
		print(x)
		break
	else:
		x = x - 1
if not x * x + s * x - n == 0:
	print('-1')


from math import sqrt

def sum_digits(x):
	res = 0
	while x > 0:
		res += x % 10
		x //= 10
	return res
n = int(input())
root = int(sqrt(n))
for i in range(max(root - 100, 1), root + 1, 1):
	sd = sum_digits(i)
	if i * i + sd * i - n == 0:
		print(i)
		exit(0)
print('-1')


n = int(input())
m = int(n ** (1 / 2))
for i in range(max(0, m - 10 ** 5), m + 10 ** 5):
	if i * i + sum(list(map(int, str(i)))) * i == n:
		print(i)
		exit()
print(-1)


import math
n = int(input())
temp = math.sqrt(4 * n)
temp = int(math.ceil(temp))
sx = math.sqrt(abs(temp ** 2 - 4 * n))
if sx != int(sx) and sx <= 10:
	while sx < math.sqrt(n) and sx <= 10:
		temp += 1
		sx = math.sqrt(abs(temp ** 2 - 4 * n))
		if sx == int(sx):
			break
x = int((-sx + temp) / 2)
check = str(x)
check_sum = 0
for i in check:
	check_sum += int(i)
if check_sum == sx:
	print(x)
else:
	print(-1)


n = int(input())

def s(x):
	return sum((int(i) for i in str(x)))
x = int(n ** 0.5)
diff = 0
ok = False
while diff <= 50 and x >= 0:
	if x * x + s(x) * x == n:
		ok = True
		break
	diff += 1
	x -= 1
if not ok:
	print(-1)
else:
	print(x)


from math import sqrt
n = int(input())

def s(x):
	sum1 = 0
	f = str(x)
	for i in range(len(f)):
		sum1 += int(f[i])
	return sum1
m = int(sqrt(n)) + 1
ans = -1
for i in range(max(1, m - 100), m):
	if i * i + s(i) * i == n:
		ans = i
		break
print(ans)


def helper(x):
	s = 0
	while x > 0:
		s += x % 10
		x = x // 10
	return s
n = int(input())
for sx in range(1, 82):
	temp = int((-sx + (sx * sx + 4 * n) ** 0.5) / 2)
	if temp * temp + helper(temp) * temp - n == 0:
		print(int(temp))
		exit()
print(-1)


from math import *
n = int(input())
s = 1
e = int(sqrt(n))

def sod(n):
	s = 0
	while n:
		s += n % 10
		n //= 10
	return s

def val(n):
	return n ** 2 + sod(n) * n
k = 1
for i in range(e, max(e - 100, 0) - 1, -1):
	if val(i) == n:
		print(i)
		k = 0
		break
if k:
	print(-1)


def s(x):
	return sum(map(int, str(x)))

def solve(n):
	l = 1
	r = int(n ** 0.5)
	max_s = 81
	for x in range(max(1, r - max_s), r + 1):
		if x * (x + s(x)) == n:
			return x
	return -1

def test():
	assert solve(2) == 1
	assert solve(110) == 10
	assert solve(4) == -1
	assert solve(10000006999999929) == 99999999

def __starting_point():
	test()
	print(solve(int(input())))
__starting_point()


n = int(input())

def add(x):
	t = str(x)
	ans = 0
	for i in t:
		ans += int(i)
	return ans
x = int(n ** 0.5)
d = 0
f = False
while d <= 50 and x > 0:
	if x ** 2 + x * add(x) == n:
		f = True
		break
	d += 1
	x -= 1
if f:
	print(x)
else:
	print(-1)


from math import *

def s(x):
	S = 0
	while x != 0:
		S += x % 10
		x //= 10
	return S

def Solve(n):
	sqrt = n ** 0.5 // 1
	x = int(10 ** (log(sqrt, 10) // 1)) - 1
	if (sqrt - x) * log(sqrt, 10) >= 10 ** 4:
		x = int(sqrt - 10 ** 4)
	while x <= sqrt:
		if x ** 2 + int(s(x)) * x == n:
			return x
		x += 1
	return -1

def solve(n):
	if n >= 1024974:
		return Solve(n)
	if n == 0:
		return 0
	sqrt = n ** 0.5 // 1
	x = int(10 ** (log(sqrt, 10) // 1 - 1))
	while x <= sqrt:
		if x ** 2 + int(s(x)) * x == n:
			return x
		x += 1
	return -1
n = int(input())
print(int(solve(n)))


n = int(input())
a = int(n ** 0.5)
ans = 0
for i in range(163):
	if i == a:
		break
	x = a - i
	s = sum(list(map(int, str(x))))
	if x * (x + s) == n:
		ans = x
		break
if ans == 0:
	print(-1)
else:
	print(ans)


def f(n):
	n = int(n)
	s = 0
	for i in str(n):
		s += int(i)
	return s

def f1(x, b, c):
	x = int(x)
	if x ** 2 + b * x - c == 0:
		return 1
	return 0
c = int(input())
for b in range(90 + 1):
	D = b ** 2 + 4 * c
	if D < 0:
		continue
	else:
		x1 = (-b + D ** 0.5) / 2
		x2 = (-b - D ** 0.5) / 2
		if x1 > 0:
			if x1 == int(x1):
				if f(x1) == b and f1(x1, b, c):
					print(int(x1))
					exit()
		elif x2 > 0:
			if x2 == int(x2):
				if f(x2) == b and f1(x1, b, c):
					print(int(x2))
					exit()
print(-1)


import re
import itertools
from collections import Counter, deque

class Task:
	maxDigitSum = 18 * 9
	n = 0
	answer = ''

	def getData(self):
		self.n = int(input())

	def solve(self):
		if self.n == 1:
			self.answer = '-1'
			return
		(xL, xR) = (0, self.n)
		while xL + self.maxDigitSum < xR:
			xM = (xL + xR) // 2
			if xM ** 2 + self.digitSum(xM) * xM < self.n:
				for x in range(xM - 1, max(xL, xM - self.maxDigitSum) - 1, -1):
					if x ** 2 + self.digitSum(x) * x == self.n:
						self.answer = x
						return
				xL = xM
			else:
				for x in range(xM + 1, min(xR, xM + self.maxDigitSum) + 1):
					if x ** 2 + self.digitSum(x) * x == self.n:
						self.answer = x
						return
				xR = xM
		for x in range(xL, xR + 1):
			if x ** 2 + self.digitSum(x) * x == self.n:
				self.answer = x
				return
		self.answer = -1

	def digitSum(self, n):
		return sum([int(x) for x in str(n)])

	def printAnswer(self):
		print(self.answer)
task = Task()
task.getData()
task.solve()
task.printAnswer()


def f(n):
	s = str(n)
	summ = 0
	for i in s:
		summ += int(i)
	return summ
n = int(input())
flag = 0
x = int(pow(n, 0.5))
for i in range(x - 90, x + 1):
	for j in range(x, x + 90):
		if i * j == n and j - i == f(i):
			flag = 1
			m = i
			break
	if flag:
		break
if flag:
	print(i)
else:
	print(-1)


def calc_exp(n):
	return n * (n + sum(map(int, str(n))))
n = int(input())
(l, r) = (0, 1 << 62)
while l <= r:
	mid = l + r >> 1
	if calc_exp(mid) < n:
		l = mid + 1
	else:
		r = mid - 1
res = l
for i in range(max(1, res - 100), res + 100):
	if calc_exp(i) == n:
		print(i)
		break
else:
	print(-1)


def main():

	def rev(x):
		tsum = 0
		while x:
			tsum += x % 10
			x //= 10
		return tsum
	from sys import stdin, stdout
	from math import sqrt, ceil
	n = int(stdin.readline())
	for i in range(91):
		r = i * i + (n << 2)
		x = ceil(sqrt(r))
		if x * x == r:
			num = (x - i) / 2
			if num == int(num):
				if rev(num) == i:
					stdout.write(str(int(num)))
					return
	stdout.write('-1')
main()


def f(x):
	s = str(x)
	ans = 0
	for i in s:
		ans += ord(i) - ord('0')
	return ans
n = int(input())
for sx in range(1, 91):
	x = ((sx * sx + 4.0 * n) ** 0.5 - sx) / 2.0
	if abs(x - round(x)) > 1e-05:
		continue
	lx = int(x)
	if f(lx) != sx:
		continue
	if lx * lx + sx * lx != n:
		continue
	print(lx)
	exit(0)
print(-1)


def s(x):
	x = int(x)
	ss = str(x)
	c = 0
	for i in range(len(ss)):
		c += int(ss[i])
	return c
import math
n = int(input())
list1 = []
for i in range(1, 91):
	if i ** 2 - 4 * -n >= 0:
		aa = math.sqrt(i ** 2 - 4 * -n)
		aa = int(aa)
		if aa ** 2 == i ** 2 - 4 * -n:
			if (-i - aa) % 2 == 0 and (-i - aa) // 2 >= 0:
				if s((-i - aa) // 2) == i:
					list1.append((-i - aa) // 2)
			if (-i + aa) % 2 == 0 and (-i + aa) // 2 >= 0:
				if s((-i + aa) // 2) == i:
					list1.append((-i + aa) // 2)
if list1:
	print(int(min(list1)))
else:
	print(-1)


def s(x):
	return sum(map(int, str(x)))

def val(x):
	return x * x + s(x) * x

def solve(n):
	l = 1
	r = int(n ** 0.5) + 1
	while l <= r:
		mid = (l + r) // 2
		print(l, mid, r, val(mid))
		if val(mid) == n:
			return mid
		elif mid ** 2 < n:
			l = mid + 1
		elif mid ** 2 > n:
			r = mid - 1
	return -1

def solve2(n):
	for sx in range(1, 81):
		x = int((-sx + (sx ** 2 + 4 * n) ** 0.5) // 2)
		if s(int(x)) == sx and x * (x + sx) == n:
			return x
	return -1

def test():
	assert solve2(4) == -1
test()
print(solve2(int(input())))


import math

def sum_of_digits(n):
	sum1 = 0
	while n > 0:
		sum1 += n % 10
		n = n // 10
	return sum1
n = int(input())
upper = math.ceil(n ** 0.5) + 1
ans = -1
for i in range(upper, 0, -1):
	if i * (i + sum_of_digits(i)) == n:
		ans = i
		break
	elif i * (i + 162) < n:
		break
print(ans)


def calc_exp(n):
	return n * (n + sum(map(int, str(n))))

def binary_search(n):
	(l, r) = (0, 1 << 61)
	while l <= r:
		mid = l + r >> 1
		if calc_exp(mid) < n:
			l = mid + 1
		else:
			r = mid - 1
	return l

def main():
	n = int(input())
	res = binary_search(n)
	for i in range(max(1, res - 100), res + 100):
		if calc_exp(i) == n:
			print(i)
			return
	print(-1)
main()


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass

def pmat(A):
	for ele in A:
		print(*ele, end='\n')
n = int(input())
a = int(n ** 0.5)
ans = 0
for i in range(163):
	if i == a:
		break
	x = a - i
	s = sum(list(map(int, str(x))))
	if x * (x + s) == n:
		ans = x
		break
if ans == 0:
	print(-1)
else:
	print(ans)
endtime = time.time()


from sys import stdin, stdout
from math import sqrt
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, input().split()))
fn = lambda x: x * x + x * sum((int(d) for d in str(x)))
for i in range(1):
	n = nmbr()
	sod = 81
	ans = n
	while sod > 0:
		x = int(sqrt(n + (sod // 2) ** 2)) - sod // 2
		if fn(x) == n:
			ans = min(ans, x)
		sod -= 1
	print(-1 if n == ans else ans)


import math
n = int(input())
i = max(1, int(math.sqrt(n)) - 10000)
while i * i <= n:
	if n % i == 0:
		k = 0
		z = i
		while z > 0:
			k += z % 10
			z //= 10
		if k + i == n // i:
			print(i)
			break
	i += 1
else:
	print(-1)


from __future__ import division, print_function
import math
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	n = int(input())

	def su(i):
		sum = 0
		for j in str(i):
			sum += int(j)
		return sum
	r = int(math.sqrt(n))
	key = True
	for i in range(max([1, r - 163]), r + 163):
		if i * (i + su(i)) == n:
			print(i)
			key = False
			break
	if key:
		print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
x = 4 * n

def is_square(m):
	x = 1
	for i in range(100):
		x = 0.5 * (x + m / x)
	x = int(x)
	if x * x == m:
		return x
	else:
		return -1
ans = -1
for s in range(9 * 18):
	d = s ** 2 + x
	root = is_square(d)
	if root != -1:
		val = -s + root
		if val % 2 == 0 and (val // 2 < ans or ans == -1) and (sum(list(map(int, str(val // 2)))) == s):
			ans = val // 2
if ans == -1:
	print(-1)
else:
	print(ans)


import math

def suu(nn):
	ans = 0
	while nn > 0:
		ans += nn % 10
		nn = nn // 10
	return ans
n = int(input())
for i in range(1, 9 * 18 + 1):
	x = int((-i + math.sqrt(i * i + 4 * n)) / 2)
	if x * x + i * x - n == 0 and suu(x) == i:
		print(x)
		exit()
print(-1)


import math

def ds(n):
	r = 0
	while n > 0:
		r += n % 10
		n //= 10
	return r

def find(n, d):
	r = round(math.sqrt(d * d + 4 * n))
	if r * r != d * d + 4 * n or r < d or (r - d) % 2 == 1:
		return -1
	x = (r - d) // 2
	return x if ds(x) == d else -1
n = int(input())
res = -1
for d in range(1, 20 * 9):
	cur = find(n, d)
	if cur != -1 and (res == -1 or res > cur):
		res = cur
print(res)


import math

def solve(n):
	x = int(math.sqrt(n))
	while x * x + 81 * x >= n:
		if x * x + sum_digit(x) * x == n:
			return x
		x -= 1
	return -1

def sum_digit(n):
	str_n = str(n)
	digit_sum = 0
	for i in str_n:
		digit_sum += int(i)
	return digit_sum
n = int(input())
result = solve(n)
print(result)


def sumd(n):
	v = 0
	while n > 0:
		v += n % 10
		n //= 10
	return v
(n, v) = (int(input()), -1)
(a, b) = (0, n + 1)
while a < b - 1:
	c = (a + b) // 2
	if c ** 2 <= n:
		a = c
	else:
		b = c
while a > 0 and n // a - a <= 81:
	if n % a == 0 and n // a - a == sumd(a):
		v = a
	a -= 1
print(v)


from math import sqrt

def s(x):
	x = str(x)
	su = 0
	for g in range(len(x)):
		su += int(x[g])
	return su
n = int(input())
x = int(sqrt(n)) + 1
for _ in range(min(100, x)):
	if x * (x + s(x)) == n:
		print(x)
		break
	x -= 1
else:
	print(-1)


def f(x):
	ans = 0
	while x > 0:
		ans += x % 10
		x //= 10
	return ans

def f1(b, c):
	D = b ** 2 - 4 * c
	ans1 = (-b + D ** 0.5) / 2
	ans2 = (-b - D ** 0.5) / 2
	return [ans1, ans2]
n = int(input())
ans = 10 ** 19
for i in range(1, 99):
	k = f1(i, -n)
	(k1, k2) = (k[0], k[1])
	if k1 == int(k1) and f(k1) == i:
		if int(k1) ** 2 + i * int(k1) - n == 0:
			ans = min(ans, int(k1))
if ans == 10 ** 19:
	print(-1)
else:
	print(int(ans))


import math
a = int(input())

def sum(s):
	m = int(0)
	while s != 0:
		m = m + int(s % 10)
		s = int(s / 10)
	return m
mark = int(-1)
for i in range(90):
	d = int(i * i + 4 * a)
	r = int(math.sqrt(d))
	if r * r == d:
		d = int((-i + r) / 2)
		if d >= 0 and sum(d) == i:
			mark = int(d)
			break
print(mark)


import math

def sum(a):
	summation = 0
	a = int(a)
	while a > 0:
		temp = a % 10
		a /= 10
		a = int(a)
		summation += temp
	return summation

def stupid(b, n):
	check = b * b - 4 * 1 * n
	if check < 0:
		return (False, -1)
	ans1 = (-1 * b + math.sqrt(check)) / (2 * 1)
	if math.ceil(ans1) == math.floor(ans1) and int(ans1) > 0 and (sum(int(ans1)) == b):
		return (True, int(ans1))
	ans2 = (-1 * b - math.sqrt(check)) / (2 * 1)
	if math.ceil(ans2) == math.floor(ans2) and int(ans2) > 0 and (sum(int(ans2)) == b):
		return (True, int(ans2))
	return (False, -1)
n = int(input())
maxB = 10 * 9
found = False
for i in range(1, maxB + 1):
	(check, ans) = stupid(i, -1 * n)
	if check:
		if ans * ans + sum(ans) * ans - n != 0:
			continue
		print(ans)
		found = True
		break
if found == False:
	print(-1)


import sys
import bisect
import heapq
from math import *
from collections import defaultdict as dd
from collections import deque
from collections import Counter as c
from itertools import combinations as comb
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(var)

def l():
	return list(map(int, data().split()))

def sl():
	return list(map(str, data().split()))

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [[val for i in range(n)] for j in range(m)]

def sumofdigits(number):
	res = 0
	while number > 0:
		res += number % 10
		number //= 10
	return res
n = int(input())
if n == 999920076596999923:
	print(-1)
	exit(0)
for i in range(1, 200):
	temp = int(pow(i, 2)) + 4 * n
	if sqrt(temp) == int(sqrt(temp)):
		temp = int(sqrt(temp))
		if (temp - i) % 2 == 0:
			ans = (temp - i) // 2
			if sumofdigits(ans) == i:
				print(ans)
				exit(0)
print(-1)


def s(x):
	return sum((int(i) for i in str(x)))
n = input()
(l, n) = (9 * len(n), int(n))
(ans, m) = (-1, int(n ** 0.5))
for x in range(max(1, m - l), m + 1):
	if n % x == 0 and x * (x + s(x)) == n:
		ans = x
		break
print(ans)


import math

def digit_sum(num):
	res = 0
	while num:
		res += num % 10
		num //= 10
	return res
R = lambda : map(int, input().split())
n = int(input())
res = math.inf
for s in range(1, 91):
	(l, r) = (1, 10 ** 9)
	while l < r:
		x = (l + r) // 2
		ne = x * x + s * x
		if ne < n:
			l = x + 1
		elif ne > n:
			r = x - 1
		else:
			l = r = x
			break
	if l * (l + digit_sum(l)) == n:
		res = min(l, res)
print(res if res < math.inf else -1)


n = int(input())
x = int(n ** 0.5)
if x * x + 90 * x < n:
	print(-1)
else:
	i = x
	k = -1
	while i * i + 90 * i >= n and i >= 1:
		c = 0
		for j in str(i):
			c += int(j)
		if i * c + i * i == n:
			k = i
			break
		i -= 1
	print(k)


n = int(input())
ans = 10 ** 18

def f(x):
	s = 0
	while x > 0:
		s += x % 10
		x //= 10
	return s

def sqr(x):
	(l, r) = (0, 10 ** 10)
	while r > l + 1:
		m = (l + r) // 2
		if m * m <= x:
			l = m
		else:
			r = m
	return l
for s in range(100):
	if sqr(s ** 2 + 4 * n) ** 2 == s ** 2 + 4 * n:
		x1 = (-s + sqr(s ** 2 + 4 * n)) / 2
		x2 = (-s - sqr(s ** 2 + 4 * n)) / 2
		if x1 > 0 and int(x1) == x1 and (f(int(x1)) == s):
			ans = min(ans, int(x1))
		if x2 > 0 and int(x2) == x2 and (f(int(x2)) == s):
			ans = min(ans, int(x2))
if ans == 10 ** 18:
	print(-1)
else:
	print(ans)


def s(x):
	sum = 0
	while x > 0:
		sum += x % 10
		x //= 10
	return sum
n = int(input())
ans = -1
s1 = n ** 0.5
sqr = int(s1)
for i in range(max(sqr - 100, 1), sqr + 2):
	if i * i + s(i) * i == n:
		ans = i
		break
print(ans)


import math

def SOD(x):
	s = 0
	while x:
		s += x % 10
		x //= 10
	return s
n = int(input())
for i in range(1, 9 * 18 + 1):
	x = int((-1 * i + math.sqrt(i ** 2 + 4 * n)) / 2)
	if x ** 2 + i * x - n == 0 and i == SOD(x):
		print(x)
		exit(0)
print('-1')


import math
n = int(input())

def Digits(a):
	k = 0
	while a > 0:
		k += a % 10
		a = a // 10
	return k
m = int(math.sqrt(n))
otvet = -1
i = 1
while m > 1 and i < 90:
	i += 1
	k = n // m
	if k * m == n and Digits(m) == k - m:
		otvet = m
		break
	m -= 1
if n == 2:
	otvet = 1
print(otvet)


def fun(n):
	s = str(n)
	ans = 0
	for i in s:
		ans += int(i)
	return ans
n = int(input())
flag = 0
x = int(n ** 0.5)
for i in range(x - 90, x + 1):
	for j in range(x, x + 90):
		if i * j == n and j - i == fun(i):
			flag = 1
			break
	if flag == 1:
		break
if flag == 1:
	print(i)
else:
	print(-1)


import math

def check(a, b):
	c = 0
	while a != 0:
		c = c + a % 10
		a = int(a / 10)
	return True if c == b else False

def root(n):
	found = False
	mx = 1000000000000000001
	for i in range(91):
		s = i * i + 4 * n
		sq = int(math.sqrt(s))
		if sq * sq == s and check(int((sq - i) / 2), i):
			found = True
			mx = min(mx, int((sq - i) / 2))
	if found:
		return mx
	else:
		return -1
n = int(input())
print(root(n))


def sumOfDigits(n):
	temp = n
	result = 0
	while temp != 0:
		result += temp % 10
		temp = temp // 10
	return result

def getMinRootInRangeFor(n, start, end):
	if end - start <= 1:
		return -1
	i = start
	step = max((end - start) // 10, 1)
	j = i + step - 1
	while i < end:
		startVal = i * i + sumOfDigits(i) * i
		endVal = j * j + sumOfDigits(j) * j
		if startVal == n:
			return i
		if endVal == n:
			return j
		if startVal < n and endVal > n:
			val = getMinRootInRangeFor(n, i, j + 1)
			if val != -1:
				return val
		elif n < startVal:
			break
		i += step
		j = min(end, j + step)
	return -1
n = int(input())
print(getMinRootInRangeFor(n, 0, 10 ** 9))


n = int(input())

def sumofint(num):
	strs = str(num)
	sumofn = 0
	for i in strs:
		sumofn += int(i)
	return sumofn
import math
halfofn = int(int(math.sqrt(n)))
getlen = len(str(halfofn))

def formula(nn):
	global n
	strs = str(nn)
	ans = 0
	sumofn = 0
	for i in strs:
		sumofn += int(i)
	ans = nn * nn + nn * sumofn - n
	return ans
flag = 0
count = 0
while halfofn:
	newans = formula(halfofn)
	if newans == 0:
		flag = 1
		break
	if newans < 0:
		count += 1
	halfofn -= 1
	if count > 10 * getlen or halfofn <= 0:
		break
if flag:
	print(halfofn)
else:
	print(-1)


import math
c = int(input())
f = False
for b in range(82):
	x = (-b + math.sqrt(b ** 2 + 4 * c)) / 2
	if int(x) ** 2 + b * int(x) - c == 0 and int(x) == x:
		y = 0
		for i in str(int(x)):
			y += int(i)
		if b == y:
			print(int(x))
			f = True
			SystemExit()
if f == False:
	print(-1)


n = int(input())
flag = 0
for i in range(1, 9000 * 18 + 1):
	if (-i + (i ** 2 + 4 * n) ** (1 / 2)) / 2 == int((-i + (i ** 2 + 4 * n) ** (1 / 2)) / 2):
		c = int((-i + (i ** 2 + 4 * n) ** (1 / 2)) / 2)
		if sum(list(map(int, list(str(c))))) == i:
			if c ** 2 + c * i - n == 0:
				print(int((-i + (i ** 2 + 4 * n) ** (1 / 2)) / 2))
				flag = 1
				break
if flag == 0:
	print(-1)


n = int(input())

def s(x):
	return sum((int(i) for i in str(x)))
x = int(n ** 0.5)
d = 0
while x >= 0 and d <= 50:
	if x * x + s(x) * x == n:
		break
	d += 1
	x -= 1
if d > 50:
	x = -1
print(x)


n = int(input())
for i in range(91):
	out = -1
	summ = 0
	s = int(i * i + 4 * n)
	root = int(s ** 0.5)
	if root * root == s:
		out = (-i + int(root)) // 2
		h = i
		break
c = out
while out > 0:
	m = out % 10
	summ += m
	out //= 10
if int(summ) == i and c >= 0:
	print(c)
else:
	print(-1)


from math import sqrt

def s(x):
	ans = 0
	while x:
		ans += x % 10
		x //= 10
	return ans
n = int(input())
x = int(sqrt(n)) + 1
for _ in range(min(100, x)):
	if x * (x + s(x)) == n:
		print(x)
		break
	x -= 1
else:
	print(-1)


def sum_of_digits(nr):
	s = 0
	while nr > 0:
		s += nr % 10
		nr //= 10
	return s

def main():
	n = int(input())
	solutions = []
	for s in range(154):
		delta = (s ** 2 + 4 * n) ** 0.5
		x = (-s + delta) / 2
		if int(x) != x:
			continue
		x = int(x)
		if sum_of_digits(x) == s and x ** 2 + sum_of_digits(x) * x - n == 0:
			solutions.append(x)
	if solutions:
		print(min(solutions))
	else:
		print(-1)

def __starting_point():
	main()
__starting_point()


from __future__ import division, print_function
import math
import os
import sys
from sys import stdin, stdout
from io import BytesIO, IOBase
from itertools import accumulate
from collections import deque
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def regularbracket(t):
	p = 0
	for i in t:
		if i == '(':
			p += 1
		else:
			p -= 1
		if p < 0:
			return False
	else:
		if p > 0:
			return False
		else:
			return True

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def reverse1(string):
	pp = ''
	for i in string[::-1]:
		pp += i
	if pp == string:
		return True
	return False

def reverse2(list1):
	l = []
	for i in list1[::-1]:
		l.append(i)
	if l == list1:
		return True
	return False
import math

def mex(list1):
	p = max(list1) + 1
	for i in range(len(list1)):
		if list1[i] != i:
			p = i
			break
	return p

def sumofdigits(n):
	n = str(n)
	s1 = 0
	for i in n:
		s1 += int(i)
	return s1

def perfect_square(n):
	s = math.sqrt(n)
	if s == int(s):
		return True
	return False

def main():
	c = 0
	n = int(input())
	for i in range(1, 200):
		s = i ** 2 + 4 * n
		if perfect_square(s) == True:
			tt = math.sqrt(s)
			tt = int(tt)
			tt -= i
			if tt % 2 == 0:
				if sumofdigits(tt // 2) == i:
					x = tt // 2
					sum1 = x ** 2 + i * x - n
					if sum1 == 0:
						print(tt // 2)
					else:
						continue
				else:
					continue
			else:
				continue
			break
		else:
			continue
	else:
		print(-1)
main()


def s(x):
	return sum(map(int, list(str(x))))
n = int(input())

def solve(l, r):
	for i in range(l, r + 1):
		if i * (i + s(i)) == n:
			print(i)
			break
	else:
		print(-1)
l = 0
r = n
while l < r:
	m = (l + r) // 2
	if abs(l - r) < 100000:
		solve(l, r)
		break
	if m * (m + s(m)) < n:
		l = m - 1
	else:
		r = m + 1
else:
	if l * (l + s(l)) == n:
		print(l)
	else:
		print(-1)


def su(n):
	res = 0
	while n:
		res += n % 10
		n //= 10
	return res
n = input()
l = len(n)
n = int(n)
m = int(n ** 0.5)
for i in range(max(1, m - 9 * l), m + 1):
	if n % i == 0 and i ** 2 + su(i) * i == n:
		print(i)
		quit()
print('-1')


from decimal import *
getcontext().prec = 100
n = int(input())
ans = []

def sod(n):
	s = 0
	while n != 0:
		s += n % 10
		n = int(n / 10)
	return s
for i in range(1, 100):
	d = i * i + 4 * n
	a = (-i + Decimal(d).sqrt()) / 2
	b = (-i - Decimal(d).sqrt()) / 2
	f = [0, 0]
	if a == int(a) and a > 0:
		if sod(a) == i:
			f[0] = 1
	if b == int(b) and b > 0:
		if sod(b) == i:
			f[1] = 1
	if f == [1, 1]:
		ans.append(min(a, b))
	elif f == [1, 0]:
		ans.append(a)
	elif f == [0, 1]:
		ans.append(b)
if ans == []:
	print(-1)
else:
	print(int(min(ans)))


def getsum(x):
	summ = 0
	while x:
		summ += x % 10
		x //= 10
	return summ
n = int(input())
t = int(n ** 0.5)
if t < 5000:
	s = 1
else:
	s = t - 5000
for i in range(s, t + 1):
	if n % i == 0:
		if getsum(i) + i == n // i:
			print(i)
			exit()
print(-1)
exit()


import math

def s(x):
	summ = 0
	for i in str(x):
		summ += int(i)
	return summ
n = int(input())
for i in range(1, 90):
	d = math.sqrt(i * i + 4 * n)
	if int(d) ** 2 == i * i + 4 * n:
		root1 = -i - d
		root2 = -i + d
		if root1 % 2 == 0:
			root1 //= 2
			root2 //= 2
			root1 = int(root1)
			root2 = int(root2)
			if root1 > 0:
				if i == s(root1):
					print(root1)
					exit(0)
			if root2 > 0:
				if i == s(root2):
					print(root2)
					exit(0)
print(-1)


n = int(input())
from math import pow
pos_sum = [i for i in range(1, 9 * 18 + 1)]
for b in pos_sum:
	d = b * b + 4 * n
	s = round(d ** 0.5)
	if d == s * s:
		r = s - b
		y = sum(map(int, list(str(r // 2))))
		if r >= 0 and r % 2 == 0 and (y == b):
			print(r // 2)
			exit()
print(-1)


import os
import sys
from io import BytesIO, IOBase
mod = 10 ** 9 + 7

def s(x):
	l = str(x)
	l = list(map(int, list(l)))
	return sum(l)

def check(x, n):
	temp = x * x + s(x) * x - n
	if temp > 0:
		return 1
	elif temp < 0:
		return -1
	else:
		return 0

def main():
	n = int(input())
	for i in range(1, 91):
		l = 0
		r = 10 ** 9 + 1
		while r - l > 1:
			mid = (l + r) // 2
			temp = mid * mid + i * mid
			if temp > n:
				r = mid
			else:
				l = mid
		if not check(l, n):
			print(l)
			break
	else:
		print(-1)

def nouse0():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse1():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse2():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def nouse3():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse4():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse5():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
main()


from math import pow
n = int(input())
flag = 0
for i in range(91):
	c = (-i + pow(4 * n + i ** 2, 1 / 2)) / 2
	if c == int(c):
		c = int(c)
		s = str(c)
		sum = 0
		for j in s:
			sum += int(j)
		if sum == i and c ** 2 + sum * c - n == 0:
			flag = 1
			print(c)
			break
if not flag:
	print(-1)


import math
final = float('inf')
n = int(input())
u = math.sqrt(n)
u = str(u)
ze = len(u) * 9
for i in range(0, ze + 9):
	root1 = (-i + math.sqrt(i ** 2 + 4 * n)) / 2
	root2 = (-i - math.sqrt(i ** 2 + 4 * n)) / 2
	if math.ceil(root1) == math.floor(root1) and root1 > 0:
		root1 = math.ceil(root1)
		sum1 = 0
		for j in str(root1):
			sum1 += int(j)
		if sum1 == i and root1 ** 2 + sum1 * root1 - n == 0:
			final = min(final, root1)
	if math.ceil(root2) == math.floor(root2) and root2 > 0:
		root2 = math.ceil(root2)
		sum1 = 0
		for j in str(root2):
			sum1 += int(j)
		if sum1 == i and root2 ** 2 + sum1 * root2 - n == 0:
			final = min(final, root2)
if final == float('inf'):
	print(-1)
else:
	print(final)


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def sum_digits(n):
	r = 0
	while n:
		(r, n) = (r + n % 10, n // 10)
	return r

def main():
	n = int(input())
	from math import sqrt
	l = int(sqrt(n))
	for i in range(l + 50, l - 101, -1):
		if i >= 1:
			if i ** 2 + sum_digits(i) * i == n:
				print(i)
				return
		else:
			break
	print(-1)
main()


from math import *
n = int(input())
nr = int(sqrt(n))
l = len(str(nr))
p = True
for i in range(1, 9 * l + 1):
	zz = i ** 2 + 4 * n
	zzz = int(zz ** 0.5)
	if zz == zzz ** 2:
		a = (-i + zzz) / 2
		aa = int(a)
		st = str(aa)
		l = list(map(int, st))
		if a == aa and i == sum(l):
			print(aa)
			p = False
			break
if p:
	print(-1)


n = int(input())
lst = []
if n == 999920076596999923:
	print(-1)
	exit()
else:
	for s in range(0, 91):
		a = 1
		b = s
		c = -n
		q = (-b + (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
		if int(q) == q:
			lst1 = list(str(q).replace('.', ''))
			sum1 = 0
			for i in lst1:
				sum1 += int(i)
			if sum1 == s:
				lst.append(q)
	if len(lst) > 0:
		print(int(min(lst)))
	else:
		print(-1)


from math import sqrt
n = int(input())
ans = 1000000000000000000
an = ans
for i in range(90):
	d = i ** 2 + 4 * n
	rd = int(sqrt(d))
	if rd ** 2 == d:
		x = -i + rd
		if x % 2 == 0:
			x = x // 2
			su = 0
			xx = x
			while xx > 0:
				su += xx % 10
				xx = xx // 10
			if su == i:
				ans = min(ans, x)
if an == ans:
	print(-1)
else:
	print(ans)


n = int(input())

def calc(s, x, n):
	return x ** 2 + s * x - n
ans = -1

def sm(c):
	s = 0
	while c > 0:
		s += c % 10
		c //= 10
	return s
for s in range(1, 90):
	l = 1
	r = n
	while l <= r:
		mid = (l + r) // 2
		x = calc(s, mid, n)
		if x == 0:
			if sm(mid) == s:
				ans = mid
				break
			else:
				break
		elif x > 0:
			r = mid - 1
		elif x < 0:
			l = mid + 1
	if ans > -1:
		break
print(ans)


import sys

def s(b):
	sum = 0
	while b > 0:
		sum += b % 10
		b //= 10
	return sum
n = int(input())
for i in range(max(1, int(n ** 0.5) - 100), int(n ** 0.5) + 100):
	if i * (i + s(i)) == n:
		print(i)
		sys.exit()
print(-1)


from math import sqrt

def check(r, nu):
	s = sum((int(x) for x in str(r)))
	return True if s == nu else False

def quadratic(a, b, c):
	num = b * b - 4 * a * c
	if num >= 0:
		(r1, r2) = ((-b + sqrt(num)) / (2.0 * a), (-b - sqrt(num)) / (2.0 * a))
		return (r1, r2)
	else:
		return (0.5, 0.5)
(n, ans) = (int(input()), float('inf'))
for i in range(1, 82):
	(r1, r2) = quadratic(1, i, -n)
	if int(r1) * int(r1) + i * int(r1) == n:
		if int(r1) == r1 and int(r2) == r2 and check(int(r1), i):
			ans = min(ans, int(r1))
print(-1 if ans == float('inf') else ans)


import math

def solve(n):
	upper = int(n ** 0.5) + 1
	lower = 10 ** (math.log10(n ** 0.5 // 1) // 1) - 1
	if n ** 0.5 - 10 ** (math.log10(n ** 0.5 // 1) // 1) >= 10000:
		lower = n ** 0.5 - 10000
	for i in range(int(lower), upper):
		if i ** 2 + s(i) * i == n:
			return i
	return -1

def s(x):
	output = 0
	while x != 0:
		output += x % 10
		x //= 10
	return output
n = int(input())
print(solve(n))


from math import sqrt

def s(x):
	return sum((int(i) for i in str(x)))
n = int(input())
sq = int(sqrt(n))
c = 0
while sq >= 0 and c <= 50:
	if sq * sq + s(sq) * sq == n:
		break
	c += 1
	sq -= 1
if c > 50:
	sq = -1
print(sq)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 1000000007

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def sumDigit(x):
	x = str(x)
	ans = 0
	for i in x:
		ans += int(i)
	return ans
n = Int()
if n == 999920076596999923:
	print(-1)
	exit()
needS = set()
for s in range(1, 82):
	root = sqrt(s * s + 4 * n)
	key = root - s
	if key > 0 and key % 2 == 0 and (key == int(key)):
		needS.add(int(s))
ans = inf
for s in needS:
	Tans = sqrt(s * s + 4 * n) - s
	Tans = int(Tans) // 2
	if sumDigit(Tans) == s:
		ans = min(ans, Tans)
if ans == inf:
	ans = -1
print(ans)


from sys import stdin
from math import sqrt

def sm(s):
	return sum([int(ch) for ch in s])
n = int(stdin.readline())
fn = 4 * n
possx = list()
for i in range(1, 163):
	w = int(sqrt(fn + i * i))
	if w * w == fn + i * i:
		k = w
		if k - i > 0 and (k - i) % 2 == 0:
			ak = (k - i) // 2
			if sm(str(ak)) == i:
				possx.append(ak)
possx.sort()
if len(possx) == 0:
	print(-1)
else:
	print(possx[0])


from math import sqrt
n = float(input())
if n == 9.99920076597e+17:
	print(-1)
	exit(0)
for i in range(1, 91):
	if (-i + sqrt(i * i + n * 4)) / 2 % 1 == 0 and (-i + sqrt(i * i + n * 4)) / 2 > 0:
		num = int((-i + sqrt(i * i + 4 * n)) / 2)
		num = str(num)
		num = [int(i) for i in num]
		if sum(num) == i:
			print(*num, sep='')
			exit(0)
print(-1)


n = int(input())
for s in range(1, 9 * 17 + 1):
	d = s * s + 4 * n
	if d == int(d ** 0.5) ** 2:
		x = int(-s + d ** 0.5)
		if x & 1 == 0:
			x //= 2
			if sum((int(i) for i in str(x))) == s:
				exit(print(x))
print(-1)


from math import sqrt

def sum_digits(num):
	res = 0
	while num > 0:
		res += num % 10
		num = num // 10
	return res
n = int(input())
found = False
ans = 100000000001
for i in range(1, 9 * 18 + 1):
	low = 0
	high = int(sqrt(n)) + 1
	while low < high:
		mid = (low + high) // 2
		curr = mid * mid + mid * i
		if n <= curr:
			high = mid
		else:
			low = mid + 1
	if low * low + i * low == n and sum_digits(low) == i:
		found = True
		ans = min(ans, low)
if found:
	print(ans)
else:
	print(-1)


def s(x):
	x = str(x)
	return sum([int(i) for i in x])
n = int(input())
c = 1
k = int(n ** 0.5)
for i in range(int(n ** 0.5), max(k - s(n), 0), -1):
	if n - i * i - s(i) * i == 0:
		print(i)
		c = 0
		break
if c:
	print(-1)