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
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <cstdlib>
#include <stdio.h>
#include <vector>
#include <math.h>
#define REP(i,n) for (int i = 1; i <= n; i++)


using namespace std;

typedef unsigned long long ull;

typedef long long ll;
typedef vector<vector<ll> > matrix;
const int K = 2;

// computes A * B
matrix mul(matrix A, matrix B, ll MOD)
{
    matrix C(K+1, vector<ll>(K+1));
    REP(i, K) REP(j, K) REP(k, K)
        C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
    return C;
}

// computes A ^ p
matrix pow(matrix A, int p, ll MOD)
{
    if (p == 1)
        return A;
    if (p % 2)
        return mul(A, pow(A, p-1, MOD), MOD);
    matrix X = pow(A, p/2, MOD);
    return mul(X, X, MOD);
}

// returns the N-th term of Fibonacci sequence
ll fib(int N, ll MOD)
{
    // create vector F1
    vector<ll> F1(K+1);
    F1[1] = 1;
    F1[2] = 1;

    // create matrix T
    matrix T(K+1, vector<ll>(K+1));
    T[1][1] = 0, T[1][2] = 1;
    T[2][1] = 1, T[2][2] = 1;

    // raise T to the (N-1)th power
    if (N == 1)
        return 1;
    T = pow(T, N-1, MOD);

    // the answer is the first row of T . F1
    ll res = 0;
    REP(i, K)
        res = (res + T[1][i] * F1[i]) % MOD;
    return res;
}

map<int,list<ull>> poczatkowe = {
		{0, {0,15,30,45}},
		{1, {1,2,8,19,22,28,41,59}},
		{2, {3,36,54,57}},
		{3, {4,7,13,26,44,46,47,53}},
		{4, {9,12,18,51}},
		{5, {5,10,20,25,35,40,50,55}},
		{6, {21,39,42,48}},
		{7, {14,16,17,23,34,37,43,56}},
		{8, {6,24,27,33}},
		{9, {11,29,31,32,38,49,52,58}}
	};

map<int,list<ull> > od_konca = {
		{1, {}},
		{2, {}},
		{3, {}},
		{4, {}},
		{5, {}},
		{6, {}},
		{7, {}},
		{8, {}},
		{9, {}},
		{10, {}},
		{11, {}},
		{12, {}},
		{13, {}},
		{14, {}},
		{15, {}},
		{16, {}},
		{17, {}},
		{18, {}},
		{19, {}}
	};

ull okresy[] = {0,60,300,1500,15000,150000,
				1500000, 15000000, 150000000, 1500000000, 15000000000,
				150000000000, 1500000000000, 15000000000000, 150000000000000, 15000000000000000,
				1500000000000000000, 1500000000000000000};

bool czyTaSamaOdKonca(string koncowka, ull liczba, int dlugosc) { //sprawdza zawsze pierwszą cyfrę liczby
	liczba = liczba / pow(10,dlugosc-1);
	ull at = (ull) koncowka.at(koncowka.length()-dlugosc) - '0';
	if(at==liczba)
		return true;
	else
		return false;
}

int main() {
	string input;
	cin >> input;

	int last = (int)input.at(input.length()-1) - '0';
	
	od_konca[1] = poczatkowe[last];
	int rozmiar_konca = input.length();
	int aktualna_od_konca = 1;

	ull akt_rozw;
	ll akt_fib;
	while(od_konca[rozmiar_konca+1].size()==0) {
		if(od_konca[aktualna_od_konca].size()==0) {
			if(od_konca[aktualna_od_konca+1].size()==0) {
				cout << "NIE";
				return 0;
			}
			else aktualna_od_konca++;
		}
		akt_rozw = od_konca[aktualna_od_konca].front();
		od_konca[aktualna_od_konca].pop_front();
		akt_fib = fib(akt_rozw,pow(10,aktualna_od_konca));
		if(czyTaSamaOdKonca(input,akt_fib, aktualna_od_konca)) {
			od_konca[aktualna_od_konca+1].push_back(akt_rozw);
			akt_rozw+=okresy[aktualna_od_konca];
			while(akt_rozw<okresy[aktualna_od_konca+1]) {
				od_konca[aktualna_od_konca+1].push_back(akt_rozw);
				akt_rozw+=okresy[aktualna_od_konca];
			}
		}
	}

	cout <<od_konca[aktualna_od_konca+1].front() <<endl;
	return 0;
}