Last 13 digits of a Fibonacci number

Given n between 1 and 10^13, find the smallest index i such that the last 13 digits of the i-th Fibonacci number equal n, or print -1.

Hard8MathNumber theoryBinary searchNo attempts yetTime limit2sMemory limit512 MB

Problem

Let FnF_n be the nn-th Fibonacci number and let Gn=Fnmod1013G_n = F_n \bmod 10^{13}. That is, GnG_n is the last 13 digits of FnF_n.

Given nn, write a program that finds the smallest ii with Gi=nG_i = n.

The Fibonacci numbers start like this.

  • F0=0F_0 = 0
  • F1=1F_1 = 1
  • F2=1F_2 = 1
  • F3=2F_3 = 2
  • F4=3F_4 = 3
  • F5=5F_5 = 5
  • F6=8F_6 = 8
  • F7=13F_7 = 13
  • F8=21F_8 = 21

Input

The first line contains an integer nn (1n10131 \le n \le 10^{13}).

Output

Print the smallest ii with Gi=nG_i = n. If no such ii exists, print -1. The answer can be greater than 101310^{13}.