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
198
199
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdint>
#include <limits>
#include <vector>
#include <map>

using namespace std;

using i64 = int64_t;

i64 PeriodOfFibModuloPowerOf10(const int power) {
  i64 period = 1;
  if (power >= 1) period *= 60;
  if (power >= 2) period *= 5;
  if (power >= 3) period *= 5;
  for (int i = 4; i <= power; ++i) {
    period *= 10;
  }
  return period;
}

struct IntMod {
  IntMod() {}
  IntMod(i64 value, i64 modulo) : value_(value), modulo_(modulo) {}
  
  IntMod& operator+= (const IntMod& r) {
    assert(modulo_ == r.modulo_);
    const i64 sum = value_ + r.value_;
    value_ = sum % modulo_;
    overflow_ = overflow_ || r.overflow_ || sum != value_;
    return *this;
  }

  // Mnożenie cyfra po cyfrze, żeby uniknąć przekroczenia zakresu int64_t.
  IntMod operator* (const IntMod& r) const {
    if (value_ < 100000000ll && r.value_ < 100000000ll) {
      IntMod l_copy(*this);
      l_copy.RawMult(r);
      return l_copy;
    }
    i64 left = value_;
    IntMod sum(0, modulo_);
    IntMod right(r);
    while (left > 0) {
      IntMod component(right);
      component.RawMult(IntMod(left % 10, modulo_));
      sum += component;
      // Mnożenie przez 10 może przekroczyć zakres int64_t. Mnożenie przez 1-9
      // jest OK.
      right.RawMult(IntMod(2, modulo_));
      right.RawMult(IntMod(5, modulo_));
      left = left / 10;
    }
    return sum;
  }

  IntMod operator+ (const IntMod& r) const {
    IntMod result(*this);
    result += r;
    return result;
  }

  i64 value_ = 0, modulo_ = 0;
  bool overflow_ = false;

 private:
  IntMod& RawMult (const IntMod& r) {
    assert(modulo_ == r.modulo_);
    const i64 product = value_ * r.value_;  // TODO: will overflow
    value_ = product % modulo_;
    overflow_ = overflow_ || r.overflow_ || product != value_;
    return *this;
  }
};

struct Matrix {
  static const int kSize = 2;

  explicit Matrix(IntMod e) {
    m_[0][0] = e;
    m_[0][1] = e;
    m_[1][0] = e;
    m_[1][1] = e;
  }

  Matrix& operator=(const Matrix&) = default;

  Matrix operator* (const Matrix& b) {
    Matrix c(m_[0][0]);
    for (int i = 0; i < kSize; ++i) {
      for (int j = 0; j < kSize; ++j) {
        IntMod sum;
        for (int k = 0; k < kSize; ++k) {
          IntMod mult = m_[i][k] * b.m_[k][j];
          if (k == 0) {
            sum = mult;
          } else {
            sum += mult;
          }
        }
        c.m_[i][j] = sum;
      }
    }
    return c;
  }

  IntMod m_[2][2];

 private:
  Matrix();
};

// TODO: use fast algorithm.
IntMod FibModulo(i64 n, i64 modulo) {
  IntMod a(0, modulo);
  IntMod b(1, modulo);
  for (i64 i = 0; i < n; ++i) {
    IntMod b_copy = b;
    b += a;
    a = b_copy;
  }
  return a;
}

IntMod FastFibModulo(i64 n, i64 modulo) {
  Matrix unit(IntMod(1, modulo));
  unit.m_[1][1] = IntMod(0, modulo);

  Matrix product(IntMod(0, modulo));
  product.m_[0][0] = IntMod(1, modulo);
  product.m_[1][1] = IntMod(1, modulo);

  while (n > 0) {
    if (n % 2 == 1) {
      product = product * unit;
    }
    unit = unit * unit;
    n >>= 1;
  }

  return product.m_[0][1];
}

void solve(const string& suffix_string) {
  const i64 whole_suffix = strtoll(suffix_string.c_str(), nullptr, 10);
  const int number_of_digits = suffix_string.length();
  // cout << "whole_suffix: " << whole_suffix << endl;
  // cout << "number_of_digits: " << number_of_digits << endl;

  i64 power_of_10 = 1;
  vector<i64> positions_in_cycle;
  positions_in_cycle.push_back(0);
  for (int digit = 1; digit <= number_of_digits; ++digit) {
    vector<i64> positions_in_bigger_cycle;
    power_of_10 *= 10;
    i64 small_period = PeriodOfFibModuloPowerOf10(digit - 1);
    i64 big_period = PeriodOfFibModuloPowerOf10(digit);
    i64 current_suffix = whole_suffix % power_of_10;
    // cout << "current_suffix: " << current_suffix << endl;
    for (i64 small_cycle = 0; small_cycle < big_period; small_cycle += small_period) {
      for (i64 pos_in_small_cycle : positions_in_cycle) {
        const i64 pos = small_cycle + pos_in_small_cycle;
        const IntMod fib = FastFibModulo(pos, power_of_10);
        if (fib.value_ == current_suffix) {
          positions_in_bigger_cycle.push_back(pos);
          // cout << "pos:" << pos << endl;
          if (digit == number_of_digits) {
            if (suffix_string[0] != '0' || number_of_digits == 1 || fib.overflow_) {
              cout << pos << endl;
              return;
            }
          }
        }
      }
    }
    positions_in_cycle.swap(positions_in_bigger_cycle);
    positions_in_bigger_cycle.clear();
  }
  cout << "NIE" << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  /*
  for (int i = 0; i < 40; ++i) {
    cout << FastFibModulo(i, 10000000000).value_ << endl;
  }
  return 0;
  */

  string suffix;
  while (cin >> suffix) {
    solve(suffix);
  }
}