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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <iostream>
#include <utility>
#include <iomanip>
#include <array>
#include <cerrno>
#include <vector>
#include <algorithm>


using namespace std;

//array<int,10> pow_10 = {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};

template <class T>
T power(T a, uint64_t ex)
{
    T r;
    if (ex==0)
    {
        T r(1);
        return r;
    }
    else
    {
        if (ex%2==1) return a*power(a,ex-1);
        else
        {
            T t=power(a,ex/2);
            return t*t;
        }
    }
}



class biggestbignum
{
    const static uint32_t M=1000000000;
    uint32_t a,b;
public:
    biggestbignum ( uint32_t a_, uint32_t b_ ): a(a_), b(b_) {};
    biggestbignum ( uint32_t a_): a(a_), b(0) {};

    biggestbignum operator * (const biggestbignum &other) const
    {
        uint64_t A = a *(uint64_t) other.a;
        uint64_t B = (a *(uint64_t) other.b) + (b *(uint64_t) other.a) + A/M;
        return biggestbignum ( A%M, B%M );
    }
    biggestbignum operator + (const biggestbignum &other)
    {
        biggestbignum res( (a+other.a), (b+other.b) );
        res.b+=res.a/M;
        res.b%=M;
        res.a%=M;
        return res;
    }
    int cyfra(int g)//g ta cyfra, cyfra jednosci to 0
    {
        if (g<9)
            return (a/power(10,g))%10;
        else
            return  (b/power(10,g-9))%10;

    }

    friend ostream& operator<<(ostream& os, const biggestbignum& x);

};

ostream& operator<<(ostream& os, const biggestbignum& x)
{
    if (x.b>0)
        os<<x.b<<setw(9)<<std::setfill ('0');
    os<<x.a;//<<" hi "<<x.b<<" lo "<<x.a;
    return os;
}

class symetric_matrix
{
public:
    biggestbignum d1,d2,c;
    symetric_matrix (biggestbignum d1_,biggestbignum d2_, biggestbignum c_ ): d1(d1_),d2(d2_),c(c_) {};
    symetric_matrix (int x): d1(x),d2(x),c(0) {};
    symetric_matrix (): d1(1),d2(1),c(0) {};
   // ~symetric_matrix () { cout<<c<<" bla"<<endl;};

    friend  symetric_matrix operator * (const symetric_matrix& A, const symetric_matrix& B);

    friend ostream& operator<<(ostream& os, const biggestbignum& x);
};

symetric_matrix  operator * (const symetric_matrix& A, const symetric_matrix& B)
{
    return symetric_matrix {  A.d1*B.d1 + A.c*B.c,A.d2*B.d2 + A.c*B.c, A.d1*B.c + A.c*B.d2 };

}


pair<long long, long long> next (pair<long long, long long> x)
{
    return make_pair( (x.first+x.second)%1000000000,x.first );
}


vector< uint64_t> okresy_k;
vector <uint8_t> cyfry;
vector< symetric_matrix> okresy_fib;
bool znaleziono=false;

void szukaj ( uint64_t k, symetric_matrix Fk, int g  ) //szukaj gtej cyfry
{
    symetric_matrix okres=okresy_fib[g];
    symetric_matrix F2=Fk;
    for (int i=0;i<10;i++)
    {
        if (F2.c.cyfra(g) == cyfry[g])
        {
            if (g==cyfry.size()-1)
            {
                cout <<k+okresy_k[g]*i<<endl;
                znaleziono = true;
                return;
            }
            else
            {
                szukaj( k + okresy_k[g]*i, F2 ,g+1);
                if (znaleziono) return;
            }
        }
        F2=F2*okres;
    }

};


int czyt()
{

    cyfry.clear();
    okresy_fib.clear();
    okresy_k.clear();
    znaleziono=false;
    char c;
    while(cin.get(c) && isalnum(c))
    {
       cyfry.push_back(c-'0');
    }
    if (cyfry.empty()) return 0;

    reverse( begin(cyfry), end(cyfry) );

    int koncowka = 0;
    int d=1;
    for (int i=0;i<3&&i<cyfry.size();++i)
    {
        koncowka+=cyfry[i]*d;
        d*=10;
    }

    okresy_k.push_back(1);
    okresy_k.push_back(60);
    okresy_k.push_back(300);
    okresy_k.push_back(1500);  //[3]


    symetric_matrix F1 {1,0,1},F;
    okresy_fib.emplace_back(F1);
    F = power(F1,60);
    okresy_fib.emplace_back(F);
    F = power(F,5);  //300=5*60
    okresy_fib.emplace_back(F);
    F = power(F,5);  //1500=5*300
    okresy_fib.emplace_back(F); //[3]
    //F.c=0;


    okresy_fib.resize(19);
    okresy_k.resize(19);
    for (int i=4;i<=18;i++)
    {
        okresy_k[i]=okresy_k[i-1]*10;
        okresy_fib[i]=power(okresy_fib[i-1],10);
    }

    int a=1;
    int b=0;


    if (cyfry.size()<=3){
        for (int k=1; k<=1651 ; k++   )
        {
            if (koncowka==a%d && k>101)
            {    //fufry 0,1,3 dopasowane, szukamy 3.
                    cout<<k<<endl;
                    return 0;
            }
            int tmp = a;
            a=(a+b)%1000;
            b=tmp;
        }
    }else {
        for (int k=1; k<=1650 ; k++   )
        {
            if (koncowka==a && k>101)
            {    //fufry 0,1,3 dopasowane, szukamy 3.
               szukaj( k, power(F1,k), 3  );
               if (znaleziono)
                {
                    return 0;
                }
            }
            int tmp = a;
            a=(a+b)%1000;
            b=tmp;
        }
    }

    cout<<"NIE"<<endl;



    return 0;
}


int main()
{
  // for (;;)
    czyt( );

}