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
200
201
202
#include <cassert>
#include <vector>
#include <cstdint>
#include <iostream>
#include <sstream>
using namespace std;

namespace {
  template <typename T, typename U> constexpr T power(T base, U exponent)
  {
    return exponent == 0 ? 1 : base * power(base, exponent-1);
  }

  class Mod1e18 {
    private:
      using type = uint64_t;
      using half_type = uint32_t;

      constexpr static auto half_mod = power(half_type{10}, 9);
      constexpr static auto mod = power(type{10}, 18);

      type value;

    public:
      Mod1e18(): value{0}
      {
      }

      Mod1e18(type value_): value{value_ % mod}
      {
      }

      type operator ()() const
      {
        return value;
      }

      friend Mod1e18 operator*(Mod1e18 const& lhs, Mod1e18 const& rhs)
      {
        auto l1 = half_type(lhs.value / half_mod);
        auto l2 = half_type(lhs.value % half_mod);
        auto r1 = half_type(rhs.value / half_mod);
        auto r2 = half_type(rhs.value % half_mod);

        auto tmp = half_type{};
        tmp += type{l1} * r2 % half_mod;
        tmp += type{l2} * r1 % half_mod;
        tmp %= half_mod;

        auto res = type{tmp} * half_mod;
        res += type{l2} * r2;
        res %= mod;

        return Mod1e18{res};
      }

      friend Mod1e18 operator+(Mod1e18 const& lhs, Mod1e18 const& rhs)
      {
        return Mod1e18{lhs.value + rhs.value};
      }

      Mod1e18& operator+=(Mod1e18 const& rhs)
      {
        value += rhs.value;
        value %= mod;
        return *this;
      }

      Mod1e18& operator*=(Mod1e18 const& rhs)
      {
        return *this = *this * rhs;
      }

      friend bool operator==(Mod1e18 const& lhs, Mod1e18 const& rhs)
      {
        return lhs.value == rhs.value;
      }

      friend bool operator!=(Mod1e18 const& lhs, Mod1e18 const& rhs)
      {
        return lhs.value != rhs.value;
      }
  };

  class FibMatrix {
    private:
      using type = Mod1e18;

      type a;
      type b;

      FibMatrix(type a_, type b_): a{a_}, b{b_}
      {
      }

    public:
      FibMatrix(): FibMatrix{0, 1}
      {
      }

      friend FibMatrix operator*(FibMatrix const& lhs, FibMatrix const& rhs)
      {
        auto a = lhs.a * rhs.a + lhs.b * rhs.b;
        auto b = lhs.b * rhs.a + (lhs.a + lhs.b) * rhs.b;
        return FibMatrix{a, b};
      }

      FibMatrix& operator*=(FibMatrix const& rhs)
      {
        return *this = *this * rhs;
      }

      template <typename T> FibMatrix power(T exponent) const
      {
        if (exponent == 0) return FibMatrix{1, 0};
        if (exponent == 1) return *this;
        auto res = (*this * *this).power(exponent / 2);
        if (exponent % 2 == 1) res *= *this;
        return res;
      }

      type operator()() const
      {
        return b;
      }

      friend bool operator==(FibMatrix const& lhs, FibMatrix const& rhs)
      {
        return lhs.a == rhs.a && lhs.b == rhs.b;
      }

      friend bool operator!=(FibMatrix const& lhs, FibMatrix const& rhs)
      {
        return lhs.a != rhs.a || lhs.b != rhs.b;
      }
  };

  constexpr uint64_t inf = -uint64_t{1};

  class Solver {
    private:
      uint64_t rem;
      unsigned len;

      vector<uint64_t> power;
      vector<uint64_t> period;
      vector<FibMatrix> skip;

      uint64_t solve(unsigned cur, FibMatrix f, uint64_t a)
      {
        if (cur == len) return a;
        while (a < period[cur+1]) {
          if (f()() % power[cur+1] == rem % power[cur+1]) {
            auto tmp = solve(cur+1, f, a);
            if (tmp != inf) return tmp;
          }
          f *= skip[cur];
          a += period[cur];
        }
        return inf;
      }

    public:
      Solver(uint64_t rem_, unsigned len_): rem{rem_}, len{len_}, power{1}, period{1, 60, 300, 1500}, skip{}
      {
        while (power.size() <= len) {
          power.push_back(10 * power.back());
        }
        while (period.size() <= len) {
          period.push_back(10 * period.back());
        }
        while (skip.size() <= len) {
          skip.push_back(FibMatrix{}.power(period[skip.size()]));
        }
      }

      uint64_t operator()()
      {
        auto res = solve(0, FibMatrix{}.power(0), 0);
        if (res != inf) res += period[len];
        return res;
      }
  };

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  string digits;
  cin >> digits;
  uint64_t rem;
  istringstream{digits} >> rem;

  auto res = Solver(rem, digits.size())();
  if (res == inf) cout << "NIE\n";
  else cout << res << '\n';

  return 0;
}