This page is still under construction.

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

Factor Solitaire

Time limit1sMemory limit128 MB

Summary
Starting from 1, repeatedly replace c by c + a where a divides c and b = c/a, paying b; find the minimum total cost to reach N.
Level

Medium7 of 10

Topics
Dynamic programming, Number theory, Math, Greedy
Solved
No attempts yet

Problem

In the game of Factor Solitaire you start with the number 11 and try to turn it into a given target number nn by repeatedly applying the following operation.

At each step, let cc be your current number. Choose two positive factors aa and bb with c=a⋅bc = a \cdot b. Add aa to your current number, so cc becomes c+ac + a. This step costs you bb points.

Repeat until your current number equals nn. Your goal is to reach nn at the minimum possible total cost.

For example, one way to reach 1515 is:

  • start with 11;
  • change 11 to 1+1=21 + 1 = 2 — cost so far 11;
  • change 22 to 2+1=32 + 1 = 3 — cost so far 1+21 + 2;
  • change 33 to 3+3=63 + 3 = 6 — cost so far 1+2+11 + 2 + 1;
  • change 66 to 6+6=126 + 6 = 12 — cost so far 1+2+1+11 + 2 + 1 + 1;
  • change 1212 to 12+3=1512 + 3 = 15 — done, total cost 1+2+1+1+4=91 + 2 + 1 + 1 + 4 = 9.

In fact 99 is the minimum possible total cost to reach 1515. Given a target number, compute this minimum total cost.

Input

The input consists of a single integer NN with 1≤N≤50000001 \le N \le 5000000. In at least half of the cases N≤50000N \le 50000, in at least another quarter of the cases N≤500000N \le 500000, and in the remaining cases N≤5000000N \le 5000000.

Output

Print a single integer: the minimum total cost to reach NN starting from 11.

Examples5

  1. Example 1

    Input
    15
    
    Expected output
    9
    
  2. Example 2

    Input
    1
    
    Expected output
    0
    
  3. Example 3

    Input
    2
    
    Expected output
    1
    
  4. Example 4

    Input
    12
    
    Expected output
    5
    
  5. Example 5

    Input
    8
    
    Expected output
    3