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
// Czy to wejdzie, czy to nie wejdzie? ;d
// Ciekawostka: pod oitimetool x=(x*2)%Mod jest 2x wolniejsze niz
// x*=2; if(x>=Mod) x -= Mod;
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

template<typename TH>
void debug_vars(const char* data, TH head){
    cerr << data << "=" << head << "\n";
}

template<typename TH, typename... TA>
void debug_vars(const char* data, TH head, TA... tail){
    while(*data != ',') cerr << *data++;
    cerr << "=" << head << ",";
    debug_vars(data+1, tail...);
}

#ifdef LOCAL
#define debug(...) debug_vars(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#endif

/////////////////////////////////////////////////////////


const int Base = 1000000000;

// dziala tylko dla poteg 10 do 10^18
LL mul_mod(LL a, LL b, LL mod){
    LL ah = a/Base, al = a%Base,
       bh = b/Base, bl = b%Base,
       mh = mod/Base;

    if(mod < Base){
        return (al*bl) % mod;
    } else {
        return ((al*bh + ah*bl)%mh*Base + al*bl) % mod;
    }

    
    /*a %= mod;
    LL res = 0;
    while(b){
        if(b & 1) res = (res+a) % mod;
        b >>= 1;
        a <<= 1;
        if(a>=mod) a -= mod;
    }
    return res;*/
}

template<LL N>
struct Matrix {
    array<array<LL, N>,N> data;
    LL Mod;

    Matrix(LL mod) : Mod(mod) {
        for(int i = 0; i < N; i++){
            for(int j = 0; j < N; j++){
                data[i][j] = 0;
            }
        }
    }
    Matrix(LL mod, vector<vector<LL>> mat) : Mod(mod) {
        for(int i = 0; i < N; i++){
            for(int j = 0; j < N; j++){
                data[i][j] = mat[i][j];
            }
        }
    }

    Matrix mul(const Matrix other) const {
        assert(Mod == other.Mod);
        Matrix result(Mod);

        for(int j = 0; j < N; j++){
            for(int i = 0; i < N; i++){
                LL res = 0;
                for(int k = 0; k < N; k++){
                    res += mul_mod(data[i][k], other.data[k][j], Mod);
                    if(res >= Mod) res -= Mod;
                }
                result.data[i][j] = res;
            }
        }
        return result;
    }

    Matrix pow(LL pwr) const {
        Matrix result(Mod), cur(*this);
        for(int i = 0; i < N; i++) result.data[i][i] = (1%Mod);
        
        while(pwr){
            if(pwr & 1) result = result.mul(cur);
            pwr >>= 1;
            cur = cur.mul(cur);
        }
        return result;
    }
};

LL fib_mod(LL n, LL mod){
    if(n <= 1){
        return n%mod;
    }
    Matrix<2> fibMat = Matrix<2>(mod, {{0,1},{1,1}}).pow(n);
    return fibMat.data[0][1];
}


// okres liczb fibonacciego modulo mod (mod - potega 10)
LL get_period(LL mod){
    switch(mod){
        case 1:    return 1;
        case 10:   return 60;
        case 100:  return 300;
        default:   return mod*3/2;
    }
}


LL N, K;

void input(){
    string s;
    cin >> s;
    N = (int)s.size();
    K = stoll(s);
    debug(N, K);
}

int main(){
    input();
    LL curMod = 1;
    vector<LL> possibilities{0};

    for(int digits = 1; digits <= N; digits++){
        curMod *= 10;
        LL prevPeriod = get_period(curMod/10),
           thisPeriod = get_period(curMod);

        vector<LL> nextPoss;
        for(LL v : possibilities){
            for(LL nxt = v; nxt < thisPeriod; nxt += prevPeriod){
                LL f = fib_mod(nxt, curMod);
                if(f == K%curMod){
                    nextPoss.push_back(nxt);
                }
            }
        }
        possibilities = nextPoss;

        debug(curMod, possibilities.size());
        if(possibilities.size() == 0){
            printf("NIE\n");
            return 0;
        }
    }

    printf("%lld\n", possibilities[0] + get_period(curMod));
}