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
#include <vector>
#include <iostream>
#include <sstream>
#include <math.h>
#include <sys/time.h>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <set>

#define FOR(i,a,b)  for(__typeof(b) i=(a);i<(b);++i)
#define REP(i,a)    FOR(i,0,a)
#define FOREACH(x,c)   for(__typeof(c.begin()) x=c.begin();x != c.end(); x++)
#define ALL(c)      c.begin(),c.end()
#define CLEAR(c)    memset(c,0,sizeof(c))
#define SIZE(c) (int) ((c).size())

#define PB          push_back
#define MP          make_pair
#define X           first
#define Y           second

#define ULL         unsigned long long
#define LL          long long
#define LD          long double
#define II         pair<int, int>
#define DD         pair<double, double>

#define VC	    vector
#define VI          VC<int>
#define VVI         VC<VI>
#define VD          VC<double>
#define VS          VC<string>
#define VII         VC<II>
#define VDD         VC<DD>

#define DB(a)       cerr << #a << ": " << a << endl;

using namespace std;

template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";}
template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); }
VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;}

ULL target;
int digits;

#define MOD 1000000000000000000ULL

ULL modMul(ULL a, ULL b){
    VI ad(18,0);
    VI bd(18,0);
    VI cd(18,0);
    REP(i,18){
        ad[i] = a%10;
        bd[i] = b%10;
        a /= 10;
        b /= 10;
    }
    REP(i,18) REP(j,18-i)
        cd[i+j] += ad[i]*bd[j];
    
    int mod = 10;
    for(int i=17; i >= 0; i--){
        cd[i] %= mod;
        mod *= 10;
    }
    ULL c = 0;
    for(int i=17; i >= 0; i--)
        c = (10*c + cd[i])%MOD;
    return c;
}

typedef tuple<ULL,ULL,ULL> Sol;

struct Matrix{
    ULL data[2][2];
    Matrix(ULL a, ULL b, ULL c, ULL d){ data[0][0] = a; data[0][1] = b; data[1][0] = c; data[1][1] = d;}
    Matrix operator*(Matrix A){ 
        Matrix ret(0,0,0,0);
        REP(i,2) REP(j,2){
            REP(k,2) ret.data[i][j] += modMul(data[i][k], A.data[k][j]);
            ret.data[i][j] %= MOD;
        }
        return ret;
    }

    Sol operator*(Sol s){
        Sol ret(get<0>(s),0ULL,0ULL);
        get<1>(ret) = (modMul(data[0][0],get<1>(s)) + modMul(data[0][1],get<2>(s)) ) % MOD;
        get<2>(ret) = (modMul(data[1][0],get<1>(s)) + modMul(data[1][1],get<2>(s)) ) % MOD;
        return ret;
    }

    Matrix pow(int k){
        Matrix ret= *this;
        REP(i,k-1)
            ret = ret*(*this);
        return ret;
    }
};

VC<Sol> baseCase(ULL target, int digits){
    VC<Sol> ret;
    ULL mod = 1; REP(i,digits) mod *= 10;
    ULL a=0,b=1;
    ULL ind=0;
    REP(i,1500){
        if (a%mod == target)
            ret.PB(make_tuple(ind,a,b));
        ind++;
        ULL c = (a+b)%MOD;
        a = b;
        b = c;
    }
    return ret;
}

int main(int argc, char *argv[]){
//while(1){
    string ns;
    cin >> ns;
    digits = SIZE(ns);
    target = stoull(ns);
    if (digits <= 3){
        VC<Sol> answers = baseCase(target,digits);
        if (!answers.empty())
            cout << get<0>(answers.back()) << endl;
        else
            cout << "NIE" << endl;
    } else{
        VC<Sol> sols = baseCase(target%1000,3);
        ULL step = 1500;
        ULL mod = 10000;
        Matrix M(0,1,1,1);
        M = M.pow(step); 
        int d = 4;
        while (d <= digits){
            VC<Sol> newSols;
            for(auto s : sols){
                REP(i,10){
                    if (get<1>(s)%mod == target%mod)
                        newSols.PB(s);
                    s = M*s;
                    get<0>(s) += step;
                }
            }
            sols = newSols;
            step *= 10;
            mod *= 10;
            M = M.pow(10);
            d++;
        }
        sort(ALL(sols));
        if (!sols.empty())
            cout << get<0>(sols.back()) << endl;
        else
            cout << "NIE" << endl;
    }
//}
    return 0;
}