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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <cstdio>
#include <cstring>

using namespace std;

typedef unsigned long long ull;

#define BMOD 1000000000
class bnum {
    public:
    unsigned int A[2];
    bnum(){}
    //bnum(const unsigned int &a) {*this = a;}
    bnum(const unsigned long long &a) {*this = a;}
    bnum(bnum &b){A[0]=b.A[0];A[1]=b.A[1];}
    inline operator unsigned long long() {
        unsigned long long r = ((unsigned long long)A[0]) + ((unsigned long long)A[1])*BMOD;
        return r;
    }
    inline void fix() {
        if ( A[0]%BMOD ) {
            A[1] += A[0]/BMOD;
            A[0]%=BMOD;
        }
        A[1]%=BMOD;
    }
    inline bnum &operator=( const unsigned int a ) {
        A[0]=a; A[1]=0;
        return *this;
    }
    inline bnum &operator=( const unsigned long long a ) {
        A[0]=a%BMOD; A[1]=(a/BMOD)%BMOD;
        return *this;
    }
    inline bnum &operator=( const bnum &a ) {
        A[0]=a.A[0]; A[1]=a.A[1];
        return *this;
    }
    inline bnum operator+( const bnum &b ) {
        bnum r = *this;
        r.A[0]+=b.A[0];
        r.A[1]+=b.A[1];
        r.fix();
        return r;
    }
    inline bnum &operator+=( const bnum &b ) {
        A[0]+=b.A[0];
        A[1]+=b.A[1];
        fix();
        return *this;
    }
    inline bnum operator*( const bnum &b ) {
        bnum r;
        unsigned long long a = ((unsigned long long)A[0])*b.A[0];
        r.A[0] = a%BMOD;
        r.A[1] = ( a/BMOD + (unsigned long long)(A[0])*b.A[1]+(unsigned long long)(A[1])*b.A[0] )%BMOD;
        return r;
    }
};

class m2 {
    public:

    bnum A[4];
    m2(){}
    m2( const m2 &a ) { *this = a; }
    m2(const bnum & a, const bnum & b, const bnum & c, const bnum & d) {
        A[0]=a; A[1]=b; A[2]=c; A[3]=d;
    }
    m2 &operator=( const m2 &a ) {
        A[0]=a.A[0];
        A[1]=a.A[1];
        A[2]=a.A[2];
        A[3]=a.A[3];
        return *this;
    }
    m2 operator*( m2 &b ) {
        m2 r;
        r.A[0] = A[0]*b.A[0] + A[1]*b.A[2] ;
        r.A[1] = A[0]*b.A[1] + A[1]*b.A[3] ;
        r.A[2] = A[2]*b.A[0] + A[3]*b.A[2] ;
        r.A[3] = A[2]*b.A[1] + A[3]*b.A[3] ;
        return r;
    }
    m2 &operator*=( m2 &b ) {
        (*this) = (*this) * b;
        return *this;
    }
    m2 operator+( m2 &b ) {
        m2 r;
        r.A[0] = A[0] + b.A[0];
        r.A[1] = A[1] + b.A[1];
        r.A[2] = A[2] + b.A[2];
        r.A[3] = A[3] + b.A[3];
        return r;
    }
    m2 &operator+=( m2 &b ) {
        A[0] += b.A[0];
        A[1] += b.A[1];
        A[2] += b.A[2];
        A[3] += b.A[3];
        return *this;
    }
};

class v2 {
    public:
        bnum A[2];
        v2(){}
        v2( const v2 &a ) { *this = a; }
        v2(const bnum & a, const bnum & b) { A[0]=a; A[1]=b; }
        v2 operator*( m2 &b ) {
            v2 r;
            r.A[0] = A[0]*b.A[0] + A[1]*b.A[2];
            r.A[1] = A[0]*b.A[1] + A[1]*b.A[3];
            return r;
        }
        v2 &operator*=( m2 &b ) {
            *this = (*this)*b;
            return *this;
        }
};

unsigned long long calc_fib_mod( int a ) {
    a--;
    m2 M( 0, 1,
          1, 1 );
    v2 V ( 0, 1 );
    while ( a ) {
        if ( a&1 )
           V *= M;
        a >>= 1;
        M *= M;
    }
    return (unsigned long long) V.A[1];
}

m2 mpow( ull a ) {
    m2 R(1, 0,
         0, 1 );
    m2 M( 0, 1,
          1, 1 );
    while ( a ) {
        if ( a&1 )
           R *= M;
        a >>= 1;
        M *= M;
    }
    return R;
}

m2 mpow10( m2 m ) {
    m *= m;
    m2 a = m;
    m *= m;
    m *= m;
    m *= a;
    return m;
}

char L[100];
ull DIV[ 100 ];
m2 M_tab[ 100 ];
ull JMP[ 100 ];

ull lookup( v2 e, ull pos, int n ) {
    if ( n<0 ) return pos;
    m2 _M = M_tab[ n ];
    m2 M( 1, 0,
          0, 1 );
    for ( int i=0; i<10; i++ ) {
        if ((ull(e.A[0])/DIV[n])%10 == L[n] ) {
            ull a = lookup( e, pos, n-1 );
            if ( a != -1 ) return a;
        }
        e *= _M;
        pos += JMP[n];
    }
    return -1;
}

int main() {
    //__uint128_t a = 500000000500LL;
    //printf("%lld\n",(long long)((a*a)%1000000000LL));
    /*m2 mm = mpow( 20 );
    v2 V( 0, 1 );
    V *= mm;
    printf("%llu\n",(ull)V.A[0]);
    return 0;//*/
    /*v2 V(0,1);
    m2 M = mpow(14998);
    V *= M;
    printf( "%llu\n", ull(V.A[0]) );
    return 0;*/

    int n;
    scanf( "%s", L );
    n = strlen( L );
    for ( int i=0; i<n; i++ ) L[i] -= '0';
    if ( n<=3 ) {
        int end = 0;
        int m = 1;
        for ( int i=0; i<n; i++ ) {
            end *= 10;
            end += L[i];
            m *= 10;
        }
        int a=0,b=1,c;
        for ( int i=0; i<1500; i++ ) {
            if ( a == end ) { printf("%d\n",i); return 0; }
            c = (a+b)%m;
            a=b;
            b=c;
        }
        printf("NIE\n");
        return 0;
    }
    M_tab[ n-4 ] = mpow( 1500 );
    DIV[ n-4 ] = 1000ULL;
    JMP[ n-4 ] = 1500ULL;
    for ( int i=n-5; i>=0; i-- ) {
        M_tab[i] = mpow10( M_tab[i+1] );
        DIV[i] = DIV[i+1]*10;
        JMP[i] = JMP[i+1]*10;
    }

    int end = ((int)L[n-1])+((int)L[n-2])*10+((int)L[n-3])*100;
    //if ( end == 0 ) T_end1[ilt1++] = 0;
    //if ( end == 1 ) T_end1[ilt1++] = 1;
    int a=0,b=1;
    v2 e( 0, 1 );
    m2 M_mul( 0, 1,
              1, 1);
    for ( int i=0; i<1500; i++ ) {
        if ( ull(e.A[0])%1000 == end ) {
            ull r = lookup( e, i, n-4 );
            if ( r != -1 ) {
                printf("%llu\n",r);
                return 0;
            }
        }
            //printf("%04d\n",i);
            //T_end1[ ilt1++ ] = i;
        e *= M_mul;
    }
    printf("NIE\n");// */
       //*/
    //for ( int i=0; i<ilt1; i++ )
    //    printf("%llu\n",T_end1[i]);
    /*for ( int i=2; )
    int a=0, b = 1;
    for ( int i )
    m2 M ( 0, 1,
           1, 1 );
    v2 V ( 0, 1 );
    M *= M;
    M *= M;
    M *= M;
    M *= M;
    M *= M;
    M *= M;
    V *= M;*/
    /*V *= M;
    V *= M;
    V *= M;
    V *= M;
    V *= M;
    V *= M;
    V *= M;*/
    //printf("%018llu\n",calc_fib_mod( 100 ));
    //bnum a( 10000100000LLU );
    //bnum b( 1000100000LLU );
    //printf("%018llu\n",(unsigned long long)(a*b));
    //int A[101]; memset( A, 0, 100*4 );
    /*long long a=1,b=1,c;
    for ( long long i=1; ; i++ ) {
        if ( a==0 && b==1 ) break;
        if ( i == 10882 )
            printf("%04d %04d\n",i,a);// A[b]++;
        c=(a+b)%10000LL; a=b; b=c;
        //if ( b==160 )
        //if ( b%100 == 33 )
    }
    //for ( int i=0; i<100; i++ ) printf("%02d %d\n",i,A[i]);*/
    return 0;
}