1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <bits/stdc++.h>
using namespace std;

const int N = 18;
typedef array<int, N> Num;

const Num Zero = {0};

Num MakeOne() {
  Num a = Zero;
  a[0] = 1;
  return a;
}

const Num One = MakeOne();

void Normalize(Num& a) {
  for (int i = 0; i < N; ++i) {
    if (i + 1 < N)
      a[i + 1] += a[i] / 10;
    a[i] %= 10;
  }
}

Num Add(const Num& a, const Num &b) {
  Num c = Zero;
  for (int i = 0; i < N; ++i)
    c[i] = a[i] + b[i];
  Normalize(c);
  return c;
}

Num Mul(const Num& a, const Num& b) {
  Num c = Zero;
  for (int i = 0; i < N; ++i)
    for (int j = 0; i + j < N; ++j)
      c[i + j] += a[i] * b[j];
  Normalize(c);
  return c;
}

typedef array<array<Num, 2>, 2> Mat;

Mat Mul(const Mat& a, const Mat& b) {
  Mat c = {Zero, Zero, Zero, Zero};
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
      for (int k = 0; k < 2; ++k)
        c[i][j] = Add(c[i][j], Mul(a[i][k], b[k][j]));
  return c;
}

Mat Pow(Mat a, unsigned long long d) {
  Mat q = {One, Zero, Zero, One};
  while (d) {
    if (d & 1)
      q = Mul(q, a);
    a = Mul(a, a);
    d >>= 1;
  }
  return q;
}

unsigned long long Fib(unsigned long long d) {
  Mat f = {One, One, One, Zero};
  Num temp = Pow(f, d)[1][0];
  unsigned long long result = 0;
  for (int i = N - 1; i >= 0; --i)
    result = result * 10 + temp[i];
  return result;
}

unsigned long long PeriodForP10(unsigned long long p10) {
  if (p10 == 10)
    return 60;
  else if (p10 == 100)
    return 300;
  else
    return p10 + p10 / 2;
}

vector<unsigned long long> Brute(unsigned long long p10, unsigned long long residue) {
  vector<unsigned long long> solutions;
  unsigned long long period = PeriodForP10(p10);
  unsigned long long a = 1, b = 0;
  for (unsigned long long k = 0; k < period; ++k) {
    if (b == residue)
      solutions.push_back(k);
    b = (a + b) % p10;
    swap(a, b);
  }
  return solutions;
}

int main() {
 
  /*
  for (int i = 0; i < 150000; ++i) {
    if (Fib(i) % 100000 == 31888)
      cerr << i << " " << Fib(i) << endl;
  }
  */

  /*
  for (int i = 0; i < 10; ++i) {
    int x = 65484 + i * 150000;
    cerr << x << " x " << Fib(x) << endl;
    int y = 140484 + i * 150000;
    cerr << y << " y " << Fib(y) << endl;
  }
  */

  // return 0;

  string buf;
  cin >> buf;
  int n = buf.length();
  unsigned long long residue = atoll(buf.c_str());
  unsigned long long p10 = 1;
  for (int i = 0; i < n; ++i)
    p10 *= 10;
  if (n < 5) {
    vector<unsigned long long> solutions = Brute(p10, residue);
    if (solutions.empty()) {
      cout << "NIE" << endl;
    } else {
      unsigned long long result = solutions[0];
      if (result < 100)
        result += PeriodForP10(p10);
      cout << result << endl;
    }
  } else {
    unsigned long long q10 = 100000;
    vector<unsigned long long> solutions = Brute(q10, residue % q10);
    assert((!solutions.empty()) == (residue % 2 == 1 or residue % 8 == 0 or residue % 32 == 2));
    if (solutions.empty()) {
      cout << "NIE" << endl;
    } else {
      while (q10 < p10) {
        /*
        cerr << q10 << endl;
        for (unsigned long long solution : solutions)
          cerr << solution << " " << Fib(solution) << endl;
        */
        unsigned long long period = PeriodForP10(q10);
        q10 *= 10;
        vector<unsigned long long> new_solutions;
        for (unsigned long long solution : solutions)
          for (int i = 0; i < 10; ++i)
            if (Fib(solution + i * period) % q10 == residue % q10)
              new_solutions.push_back(solution + i * period);
        assert(new_solutions.size() == solutions.size());
        solutions = new_solutions;
      }
      unsigned long long result = solutions[0];
      if (result < 100)
        result += PeriodForP10(p10);
      assert(Fib(result) % p10 == residue);
      cout << result << endl;
    }
  }
}