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
// Artur Kraska, II UWr

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cmath>
#include <list>
#include <set>
#include <map>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED)            ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000007

//TODO sprawdzic czy liczba nie jest za mala
using namespace std;

/***** POM *****/

long long pomnoz_longi(long long a, long long b, long long mod)
{
    long long res = 0;

    while(b > 0)
    {
        if(b%2 != 0)
        {
            res = (res + a) % mod;
        }
        b >>= 1;
        a = (a*2) % mod;
    }

    return res;
}

/***** FIBBO *****/

long long mac[2][2], jed[2][2], pom[2][2], m[2][2];

void init_fib()
{
    jed[0][0] = jed[1][1] = 1;
    mac[0][0] = mac[1][0] = mac[0][1] = 1;
}

void pomnoz(long long t1[2][2], long long t2[2][2], long long t3[2][2], long long mod)
{
    forr(i, 2)
        forr(j, 2)
            t3[i][j] = 0;

    forr(i, 2)
        forr(j, 2)
            forr(k, 2)
                t3[i][j] = (t3[i][j] + pomnoz_longi(t1[i][k], t2[k][j], mod)) % mod;
}

void poteguj(long long t1[2][2], long long t2[2][2], long long n, long long mod)
{
    if(n == 0)
    {
        pomnoz(jed, jed, t2, mod);
        return ;
    }

    poteguj(t1, t2, n/2, mod);

    pomnoz(t2, t2, pom, mod);
    if(n%2 == 1)
        pomnoz(pom, t1, t2, mod);
    else
        pomnoz(pom, jed, t2, mod);
}

long long fib(long long nr, long long mod)
{
    poteguj(mac, m, nr, mod);
    return m[0][1];
}

/***** ROZW *****/

long long n, liczba;
char slowo[10007];
list <long long> lista[20];
long long pocz[20];

void init()
{
    pocz[0] = 1;
    pocz[1] = 60;
    pocz[2] = 300;
    pocz[3] = 1500;
    FOR(i, 4, 18)
        pocz[i] = pocz[i-1]*10;

    // check
    /*
    long long pot = 10;
    FOR(i, 1, 18)
    {
        cout << fib(pocz[i], pot) << " " << fib(pocz[i]+1, pot) << " " << fib(pocz[i]+2, pot) << endl;
        pot *= 10;
    }
    */
}

int main()
{
    init_fib();
    init();
/*
    forr(i, 1500)
        cout << i << ": " << fib(i, 1000000) << endl;

    long long co_ile = 15;

    unsigned long long mod = 1000000000000000000ULL;
    forr(i, 18)
    {
        FOR(i, 0, 10)
        {
            cout << i*co_ile << ": " << fib(i*co_ile, mod) << endl;
            cout << i*co_ile+1 << ": " << fib(i*co_ile+1, mod) << endl;
            cout << i*co_ile+2 << ": " << fib(i*co_ile+2, mod) << endl;
        }

        co_ile *= 10;
    }
*/
    scanf("%s", slowo);
    n = strlen(slowo);

    forr(i, n)
        liczba = liczba*10 + slowo[i] - '0';

    lista[0].PB(0);

    long long pot = 10;
    FOR(i, 1, n)
    {
        FOREACH(it, lista[i-1])
            for(long long j=0; j < pocz[i]; j += pocz[i-1])
            {
                long long nr = *it + j;
                long long f = fib(nr, pot);
//                cout << "     sprawdza " << nr << " na " << pot << endl;
                if(f == liczba % pot)
                {
                    lista[i].PB(nr);
//                    cout << "   doklada " << nr << " do listy " << i << endl;
                }
            }
        pot *= 10;
    }
/*
    FOREACH(it, lista[n])
    {
        cout << *it << " -> " << fib(*it, pot) << endl;
    }
*/
    if(lista[n].empty())
    {
        printf("NIE\n");
    }
    else
    {
        printf("%lld\n", lista[n].front() + pocz[n]);
    }
    return 0;
}