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

#QHD0000014. Tổng bình phương tối thiểu (Squares)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng bình phương tối thiểu (Squares)

Squares

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Given a positive integer nn, a positive perfect square is an integer of the form k2k^2 for some positive integer kk, such as 1,4,9,16,…1,4,9,16,\ldots.

The same perfect square may be used more than once. Represent nn as a sum of positive perfect squares while minimizing the number of terms.

Input

The input contains one positive integer nn.

Output

Print one integer: the minimum number of positive perfect squares whose sum is nn.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le 3000.
  • Subtask 3 (50 points): 1≤n≤1041\le n\le 10^4.

Examples

Input

12

Output

3

Explanation

We have 12=4+4+4=22+22+2212=4+4+4=2^2+2^2+2^2, using 33 positive squares. It cannot be represented by one or two positive squares, so the answer is 33.