#include <bits/stdc++.h>
using namespace std;
int n;
int a[500001];
map<int, int> cnt, pos;
vector<int> ans;
struct mypair {
  int left;
  int right;
  int value;
};
mypair Mypair[500001];
int pointer = 0;
void writeans(int a, int b) {
  ans.push_back(a);
  ans.push_back(b);
  ans.push_back(a);
  ans.push_back(b);
  cnt.clear();
  pos.clear();
  pointer = 0;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (cnt.find(a[i]) != cnt.end()) {
      cnt[a[i]]++;
    } else
      cnt[a[i]] = 1;
    if (cnt[a[i]] == 4) {
      writeans(a[i], a[i]);
      continue;
    }
    if (pos.find(a[i]) == pos.end()) {
      pos[a[i]] = i;
      continue;
    }
    int left = pos[a[i]];
    pos[a[i]] = i;
    while (pointer > 0) {
      int lastl = Mypair[pointer - 1].left;
      int lastr = Mypair[pointer - 1].right;
      if (lastl < left && left < lastr) {
        writeans(Mypair[pointer - 1].value, a[i]);
        break;
      } else if (lastl >= left) {
        pointer--;
      } else {
        Mypair[pointer].left = left;
        Mypair[pointer].right = i;
        Mypair[pointer].value = a[i];
        pointer++;
        break;
      }
    }
    if (pointer == 0) {
      Mypair[pointer].left = left;
      Mypair[pointer].right = i;
      Mypair[pointer].value = a[i];
      pointer++;
    }
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
  return 0;
}
