#include <bits/stdc++.h>
using namespace std;
int a[1000005];
int b[1000005];
map<int, int> m;
map<int, int> c;
vector<pair<int, int> > v;
int cnt, n;
void add(int mid, int fi, int se) {
  b[cnt++] = v[mid].first;
  b[cnt++] = fi;
  b[cnt++] = v[mid].second;
  b[cnt++] = se;
  m.clear();
  v.clear();
  c.clear();
}
int search(int fi, int se) {
  while (!v.empty() && v.back().first >= fi) v.pop_back();
  if (!v.empty() && v.back().first < fi and v.back().second > fi) {
    add(v.size() - 1, fi, se);
  } else
    v.push_back(make_pair(fi, se));
  return 1;
}
void solve(int x) {
  if (x > n) return;
  for (int i = x; i <= n; i++) {
    if (m.count(a[i]) > 0) {
      c[a[i]]++;
      if (c[a[i]] == 4) {
        b[cnt++] = m[a[i]];
        b[cnt++] = m[a[i]];
        b[cnt++] = m[a[i]];
        b[cnt++] = m[a[i]];
        m.clear();
        v.clear();
        c.clear();
      } else {
        search(m[a[i]], i);
      }
    } else {
      m[a[i]] = i;
      c[a[i]] = 1;
    }
  }
}
int main() {
  cin >> n;
  m.clear();
  for (int i = 1; i <= n; i++) cin >> a[i];
  solve(1);
  cout << cnt << endl;
  for (int i = 0; i < cnt; i++) cout << a[b[i]] << " ";
  cout << endl;
  return 0;
}
