This page is still under construction.

Parts of this page are still being built. What you see may change.

New Maths

Time limit1sMemory limit1024 MB

Summary
Given N, find the smallest positive integer a whose carryless square (base 10 multiplication with carries discarded) equals N, or report -1.
Level

Medium7 of 10

Topics
Math, Backtracking, Implementation, Brute force
Solved
No attempts yet

Problem

"Drat!" cursed Charles. "This stupid carry bar is not working in my Engine! I just tried to calculate the square of a number, but it's wrong; all of the carries are lost."

"Hmm," mused Ada, "arithmetic without carries! I wonder if I can figure out what your original input was, based on the result I see on the Engine."

Carryless addition, denoted by ⊕\oplus, is the same as normal addition, except any carries are ignored (in base 1010). Thus, 37⊕4837 \oplus 48 is 7575, not 8585.

Carryless multiplication, denoted by ⊗\otimes, is performed using the schoolboy algorithm for multiplication, column by column, but the intermediate additions are calculated using carryless addition. More formally, let a_ma_m−1…a_1a_0a\_m a\_{m-1} \ldots a\_1 a\_0 be the digits of aa, where a_0a\_0 is its least significant digit. Similarly let b_nb_n−1…b_1b_0b\_n b\_{n-1} \ldots b\_1 b\_0 be the digits of bb. The digits of c=a⊗bc = a \otimes b are given by the following equation: [ c_k = \left( a_0 b_k \oplus a_1 b_{k-1} \oplus \cdots \oplus a_{k-1} b_1 \oplus a_k b_0 \right) \bmod{10}, ] where any a_ia\_i or b_jb\_j is considered zero if i>mi > m or j>nj > n. For example, 9⊗1 2349 \otimes 1\,234 is 9 8769\,876, 90⊗1 23490 \otimes 1\,234 is 98 76098\,760, and 99⊗1 23499 \otimes 1\,234 is 97 53697\,536.

Given NN, find the smallest positive integer aa such that a⊗a=Na \otimes a = N.

Input

The input consists of a single line with a positive integer NN, with at most 2525 digits and no leading zeros.

Output

Print, on a single line, the least positive number aa such that a⊗a=Na \otimes a = N. If there is no such aa, print '-1' instead.

Examples4

  1. Example 1

    Input
    6
    
    Expected output
    4
    
  2. Example 2

    Input
    149
    
    Expected output
    17
    
  3. Example 3

    Input
    123476544
    
    Expected output
    11112
    
  4. Example 4

    Input
    15
    
    Expected output
    -1