#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

bool dbug;
vector<int> list;
set<int> seen;
set<int> great;
set<int> good;
set<int> common;
set<int> kinda;
int counts[5];
int maxcounts[5];

void print() {
  for (int i=0;i<list.size();i++) {
    cout << list[i];
  }
  cout << "\n";
}

int codeat(int pos) {
  int answer = 0;
  for (int a=pos;a<pos+4;++a) {
    for (int b=a+1;b<pos+5;++b) {
      answer *= 2;
      answer += ((list[a]==list[b])?1:0);
    }
  }
  return answer;
}

int trials;

void recurse(int next, int fourat) {

if (dbug)  print();
  int lastcode = 0;
  trials++;
  if (trials > 20000000) {
    trials = 0;
    print();
  }
  if (next >= 4) {
    lastcode = codeat(next-5);
    if (seen.find(lastcode) != seen.end()) {
if (dbug)        cout << next-5 << " is seen\n";
      return;
    }
    if (common.find(next) != common.end()) {
      if (great.find(lastcode) == great.end()) {
if (dbug)         cout << " not great! \n";
         return;
      }
    }
    if (kinda.find(next) != kinda.end()) {
      if (good.find(lastcode) == good.end()) {
if (dbug)         cout << " not good! \n";
         return;
      }
    }
    if (fourat + 5 < next && (seen.find(0) == seen.end())) {
      return;
    }
    seen.insert(lastcode);
if (dbug)    cout << " inserting " << lastcode << "\n";
  }
  if (next == 56) {
    cout << "Solution! ";
    print();
  } else if (next >= 52) {
    list.push_back(list[next-52]);
    recurse(next+1,fourat); 
    list.pop_back();
  } else {
//  int foo[] = {0,1,2,3,4};
//  random_shuffle(foo, foo+5);
    for (int j=0;j<5;j++) {
//      int i = foo[j];
      int i = j;
      if (counts[i] < maxcounts[i]) {
        list.push_back(i);
        counts[i]++;
        recurse(next+1, ((i==4) ? next : fourat) ); 
        counts[i]--;
        list.pop_back();
      }
    }
  }
  if (next >= 5) {
    seen.erase(lastcode);
  }
if (dbug)  cout << " removing " << lastcode << "\n";
}

int main() {
  maxcounts[0] = 12;
  maxcounts[1] = 13;
  maxcounts[2] = 13;
  maxcounts[3] = 13;
  maxcounts[4] = 1;

  great.insert(0); great.insert(4); great.insert(64); great.insert(16);
  great.insert(8); great.insert(1); great.insert(128); great.insert(2);
  great.insert(32); great.insert(256); great.insert(12); great.insert(80);
  great.insert(42); great.insert(160); great.insert(136); great.insert(388);

  good.insert(0); good.insert(4); good.insert(64); good.insert(16);
  good.insert(8); good.insert(1); good.insert(128); good.insert(2);
  good.insert(32); good.insert(256); good.insert(12); good.insert(80);
  good.insert(42); good.insert(160); good.insert(136); good.insert(388);
  good.insert(512); good.insert(130); good.insert(193); good.insert(96);
  good.insert(322); good.insert(272); good.insert(18); good.insert(264);
  good.insert(257); 
  good.insert(68); 
  good.insert(396); 
  good.insert(7); 
  good.insert(338); 
  good.insert(52); 

  common.insert(5+3); common.insert(1+3); common.insert(18+3); 
  common.insert(9+3); common.insert(15+3); common.insert(20+3);

  kinda.insert(5+3);
  kinda.insert(1+3);
  kinda.insert(18+3);
  kinda.insert(9+3);
  kinda.insert(15+3);
  kinda.insert(20+3);
  kinda.insert(14+3);
  kinda.insert(19+3);
  kinda.insert(12+3);
  kinda.insert(3+3);
  kinda.insert(21+3);
  kinda.insert(4+3);
  kinda.insert(16+3);
  kinda.insert(13+3);
  kinda.insert(8+3);
  kinda.insert(7+3);
  kinda.insert(2+3);
//  kinda.insert(4+3); kinda.insert(25+3); kinda.insert(23+3);
// kinda.insert(11+3); kinda.insert(22+3); kinda.insert(24+3);
//  kinda.insert(26+3); kinda.insert(10+3); kinda.insert(17+3);


  counts[0] = 0;
  counts[1] = 1;
  counts[2] = 0;
  counts[3] = 0;
  counts[4] = 0;
  list.push_back(1);
  dbug = false;
  recurse(1,-1);
  list.pop_back();

  counts[0] = 1;
  counts[1] = 0;
  counts[2] = 0;
  counts[3] = 0;
  counts[4] = 0;
  list.push_back(0);
  dbug = false;
  recurse(1,-1);
  list.pop_back();

  counts[0] = 0;
  counts[1] = 0;
  counts[2] = 0;
  counts[3] = 0;
  counts[4] = 1;
  list.push_back(4);
  dbug = false;
  recurse(1,0);
  list.pop_back();
}
