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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll addmod( ll x, ll y, ll m )
{
  x %= m;
  y %= m;
  ll sum = x-m+y; // -m <= sum < m-1
  return sum < 0 ? sum + m : sum;
}

ll timesmod( ll x, ll y, ll m )
{
  x %= m;
  y %= m;
  ll a = x < y ? x : y; // min
  ll b = x < y ? y : x; // max
  ll product = 0;
  for (; a != 0; a >>= 1, b = addmod(b,b,m) )
    if (a&1) product = addmod(product,b,m);
  return product;
}

struct Mat {
  // row-major
  ll t[2][2];

  Mat mult(const Mat &lhs, ll m) {
    Mat ret {{{0, 0},
              {0, 0}}};
    for(size_t i = 0; i < 2; ++i)
      for(size_t j = 0; j < 2; ++j)
        for(size_t k = 0; k < 2; ++k) {
          ret.t[i][j] += timesmod((t[i][k] % m), (lhs.t[k][j] % m), m);
          ret.t[i][j] %= m; // modulo first, ask questions later; just to be sure
        }
    return ret;
  }

  Mat & operator%=(ll m) {
    for(ll *p : {&t[0][0], &t[0][1], &t[1][0], &t[1][1]})
      *p %= m;
  }

/*
b00 = a00*a00 + a01*a10   b01 = a00*a01 + a01*a11
b10 = a10*a00 + a11*a10   b11 = a10*a01 + a11*a11


b00 = a00*a00 + a01*a10   b01 = a01*(a00 + a11)
b10 = a10*(a00 + a11)     b11 = a10*a01 + a11*a11
*/
  void inplaceSqr(ll m) {
    *this %= m;
    const ll q = (t[0][0] + t[1][1]) % m;
    t[0][0] = timesmod(t[0][0], t[0][0], m) + timesmod(t[0][1], t[1][0], m);
    t[1][1] = timesmod(t[0][1], t[1][0], m) + timesmod(t[1][1], t[1][1], m);
    t[0][1] = timesmod(t[0][1], q, m);
    t[1][0] = timesmod(t[1][0], q, m);
    *this %= m; // thou shalt modulo all things
  }
};

const Mat I {{{1, 0},
              {0, 1}}};
const Mat F {{{1, 1},
              {1, 0}}};
Mat ebs(Mat A, ll e, ll m) {
  if(e == 0)
    return I;

  A %= m;

  if(e == 1)
    return A;

  if(e % 2 == 0) {
    A.inplaceSqr(m);
    return ebs(move(A), e/2, m);
  }

  // assert(e % 2 == 1);

  Mat B = A;
  A.inplaceSqr(m);
  return ebs(move(A), e/2, m).mult(B, m);
}

ll fib(ll x, ll m) {
  if(x == 0)
    return 0;
  return ebs(F, x-1, m).t[0][0];
}

ll getdig(ll x, size_t i) {
  for(; i; --i)
    x /= 10;
  return x % 10;
}

const ll cycles[] {
  60LL,
  300LL,
  1500LL,
  15000LL,
  150000LL,
  1500000LL,
  15000000LL,
  150000000LL,
  1500000000LL,
  15000000000LL,
  150000000000LL,
  1500000000000LL,
  15000000000000LL,
  150000000000000LL,
  1500000000000000LL,
  15000000000000000LL,
  150000000000000000LL,
  1500000000000000000LL,
};

int main() {
/*
  for(ll i = 0; i < 1500; ++i)
    fprintf(stderr, "%20lld %20lld\n", i, fib(i, 1000LL));
  fprintf(stderr, "\n%20lld %20lld\n", 1525LL, fib(1525, 1000000000000000000LL));
*/

  char cbuf[19];
  scanf("%s", cbuf);
  ll c;
  sscanf(cbuf, "%lld", &c);

  if(c == 0) {
    printf("0\n");
    return 0;
  }

  string sc = cbuf;

  vector<ll> targets(60), hits;
  iota(targets.begin(), targets.end(), 0);

  for(ll x : targets)
    if(c % 10 == fib(x, 10))
      hits.push_back(x);

  ll tenpow = 10;
  for(size_t i = 1; i < sc.size(); ++i) {

/*
    sort(hits.begin(), hits.end());
    for(ll x : hits)
      fprintf(stderr, "%lld\t", x);
    fprintf(stderr, "\n");
*/

    tenpow *= 10;

    targets.clear();
    targets.swap(hits);

    for(size_t j = targets.size() - 1; j < targets.size(); --j) // damn hackers!!
      for(size_t k = 1; i <= 2 ? k < 5 : k < 10; ++k)
        targets.push_back(targets[j] + k*cycles[i-1]);

/*
    sort(targets.begin(), targets.end());
    for(ll x : targets)
      fprintf(stderr, "%lld\t", x);
    fprintf(stderr, "\n\n");
*/

    for(ll x : targets) {
      ll f = fib(x, tenpow);
      if(getdig(c, i) == getdig(f, i))
        hits.push_back(x);
    }
  }

  if(hits.empty()) {
    printf("NIE\n");
    return 0;
  }

/*
  for(ll x : hits)
    fprintf(stderr, "%lld\t", x);
  fprintf(stderr, "\n");
*/

  printf("%lld\n", hits[0] + cycles[sc.size()-1]);

  return 0;
}