This page is still under construction.

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

Sum of Squares (More Huge)

Time limit0.5sMemory limit512 MB

Summary
Given n up to 10^18, print the minimum number of perfect squares (at most four) whose sum equals n.
Level

Medium7 of 10

Topics
Math, Number theory, Binary search, Implementation
Solved
No attempts yet

Problem

In 1770, Lagrange proved that every natural number can be expressed as the sum of four or fewer squares. Some natural numbers have more than one such representation. For example, 26 is the sum of 525^2 and 121^2; it can also be expressed as 42+32+124^2 + 3^2 + 1^2. Historically, the problem commonly given to mental calculation experts was to represent a natural number as a sum of four or fewer squares. In the early 1900s, one mental calculator was reported to have taken 8 seconds to find the solution 15663=1252+62+12+1215663 = 125^2 + 6^2 + 1^2 + 1^2. A harder problem took 56 seconds: 11339=1052+152+82+5211339 = 105^2 + 15^2 + 8^2 + 5^2.

Given a natural number nn, write a computer program that expresses nn as the sum of the fewest possible squares.

Input

Input comes from standard input. The input consists of one line containing a natural number nn, where 1≤n≤1,000,000,000,000,000,0001 \le n \le 1{,}000{,}000{,}000{,}000{,}000{,}000.

Output

Output goes to standard output. Print, on one line, the minimum number of squares whose sum equals nn.

Examples4

  1. Example 1

    Input
    25
    
    Expected output
    1
    
  2. Example 2

    Input
    26
    
    Expected output
    2
    
  3. Example 3

    Input
    11339
    
    Expected output
    3
    
  4. Example 4

    Input
    34567
    
    Expected output
    4