#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <utility>
#include <cctype>

using namespace std;

const size_t M = 1000003;

typedef pair<unsigned long long, size_t> myPair;
typedef vector<myPair> vecPairs;
typedef vector<vecPairs> vecVecPairs;

vecVecPairs hashTable(M);

size_t hash(unsigned long long x) {
  return x % M;
}

int main() {
  ifstream in("pi.txt");
  int c;
  size_t pos = 0, first_pos = M, second_pos = M, h;
  unsigned long long n = 0, first_n;
  vecPairs::iterator it;

  for (int i = 0; i < 9; ++i)
    n = 10 * n + (in.get() - '0');

  while (in.good()) {
    c = in.get();
    if (!isdigit(c) || !in.good())
      continue;
    
    n = (n % 1000000000ULL) * 10 + (c - '0');
    ++pos;

    h = hash(n);
    for (it = hashTable[h].begin(); it != hashTable[h].end(); ++it)
      if (it->first == n)
	break;
    if (it == hashTable[h].end())
      hashTable[h].push_back(make_pair(n, pos));
    else
      if (it->second < first_pos) {
	first_pos = it->second;
	second_pos = pos;
	first_n = n;
      }
  }

  cout << "Sanity check:" << endl
    << "hashTable[hash(1415926535)][0].second = "
    << hashTable[hash(1415926535ULL)][0].second << endl // 1
    << "hashTable[hash(8214808651)][0].second = "
    << hashTable[hash(8214808651ULL)][0].second << endl // 101
    << endl;

  cout << "first_pos = " << first_pos << endl
    << "second_pos = " << second_pos << endl
    << "first_n = " << first_n << endl;

  return EXIT_SUCCESS;
}
