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
#include <iostream>
#include <vector>
#include <string>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)
#define PB push_back

using namespace std;

typedef long long int LLI;

typedef vector < LLI > VL;
typedef vector < VL > VVL;

/*************************************************************************/

LLI getPow(int n)
{
    if (!n)
        return 1;
    else
        return 10 * getPow(n-1);
}

/*************************************************************************/

typedef VVL Matrix;

Matrix zero(int n)
    { return Matrix(n, VL(n, 0)); }

Matrix identity(int n)
{
    Matrix ans = zero(n);

    FOR(i,0,n-1)
        ans[i][i] = 1;

    return ans;
}

const LLI MOD = getPow(18);
const LLI SQRT_MOD = getPow(9);

/* returns a * b modulo MOD */
LLI smartMul(LLI a, LLI b)
{
    LLI a0 = a/SQRT_MOD, a1 = a%SQRT_MOD;
    LLI b0 = b/SQRT_MOD, b1 = b%SQRT_MOD;

    LLI x0 = (a0 * b1 + a1 * b0)%SQRT_MOD;
    LLI x1 = (a1 * b1)%MOD;

    return (x0 * SQRT_MOD + x1)%MOD;
}

/* multipies square matrices modulo MOD */
Matrix prod(const Matrix &a, const Matrix &b)
{
    int n = SIZE(a);

    Matrix ans = zero(n);

    FOR(i,0,n-1) FOR(j,0,n-1) FOR(t,0,n-1)
        ans[i][t] = (ans[i][t] + smartMul(a[i][j], b[j][t]))%MOD;

    return ans;
}

/* quick matrix exponentation modulo MOD */
Matrix pow(const Matrix &a, LLI exp)
{
    if (!exp)
        return identity(SIZE(a));
    else
    {
        Matrix temp = pow(a, exp / 2);

        temp = prod(temp, temp);

        if (exp%2)
            temp = prod(temp, a);

        return temp;
    }
}

/*************************************************************************/

/* returns n-th fibonacci number modulo MOD */
LLI getFib(LLI n)
{
    if (!n)
        return 0;
    else
    {
        Matrix M = {{1, 1}, {1, 0}};
        return pow(M, n-1)[0][0];
    }
}

/* returns pisano period for 10^n */
LLI getCycleLen(int n)
{
    switch (n)
    {
        case 1: return 60;
        case 2: return 300;
        default: return 15 * getPow(n-1);
    }
}

/*************************************************************************/

LLI solve(int n, LLI v)
{
    VL ans;

    FOR(i,0,getCycleLen(1)-1)
        if (getFib(i)%10 == v%10)
            ans.PB(i);

    FOR(i,2,n)
    {
        LLI mod = getPow(i);
        LLI lastCycle = getCycleLen(i-1);
        LLI cycle = getCycleLen(i);

        VL temp;

        for (LLI val : ans)
            for (LLI newVal = val; newVal < cycle; newVal += lastCycle)
                if (getFib(newVal)%mod == v%mod)
                    temp.PB(newVal);

        ans = temp;
    }

    if (ans.empty())
        return -1;
    else
        return ans[0];
}


/*************************************************************************/

int main()
{
    ios_base::sync_with_stdio(0);

    string s;
    cin >> s;

    int n = SIZE(s);
    LLI v = stoll(s);

    LLI ans = solve(n, v);

    if (ans == -1)
        cout << "NIE";
    else
        cout << ans + getCycleLen(n);

    return 0;
}

/*************************************************************************/