PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000014. Tối thiểu hóa sai lệch bình phương (Minimize Abs 2)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tối thiểu hóa sai lệch bình phương (Minimize Abs 2)

Minimize Abs 2

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a positive integer DD. Choose any two non-negative integers xx and yy, with no further restrictions on either number. Consider the sum of their squares and its absolute difference from DD.

Find the minimum possible value of ∣x2+y2−D∣|x^2+y^2-D| over all choices of non-negative integers x,yx,y.

Input

  • The only line contains the positive integer DD.

Output

Print one non-negative integer: the minimum value of ∣x2+y2−D∣|x^2+y^2-D|.

Subtasks

  • Subtask 1 (20 points): 1≤D≤1001\le D\le 100.
  • Subtask 2 (30 points): 1≤D≤1061\le D\le 10^6.
  • Subtask 3 (50 points): 1≤D≤2⋅10121\le D\le 2\cdot10^{12}.

Examples

Input

21

Output

1

Explanation

Choosing x=4 and y=2 yields x²+y²=16+4=20, with error |20-21|=1. No pair of non-negative integers has square sum 21, so the minimum is 1.