#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const long long linf = 1e18 + 5;
const int N = 5e5 + 5;
int n, x, last;
pair<int, int> t[N << 2];
vector<int> v;
map<int, vector<int> > w;
map<int, int> bef;
pair<int, int> update(int k, int b, int e, int x, pair<int, int> v) {
  if (e < x or x < b) return t[k];
  if (x <= b and e <= x) return t[k] = v;
  int m = b + e >> 1;
  return t[k] =
             max(update(k + k, b, m, x, v), update(k + k + 1, m + 1, e, x, v));
}
pair<int, int> query(int k, int b, int e, int x, int y) {
  if (e < x or y < b or y < x) return make_pair(0, 0);
  if (x <= b and e <= y) return t[k];
  int m = b + e >> 1;
  return max(query(k + k, b, m, x, y), query(k + k + 1, m + 1, e, x, y));
}
int main() {
  ios ::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    w[x].push_back(i);
    if (w[x].size() >= 4 and w[x][w[x].size() - 4] > last) {
      v.push_back(x);
      v.push_back(x);
      v.push_back(x);
      v.push_back(x);
      last = i;
      continue;
    }
    if (bef[x] > last) {
      pair<int, int> ans = query(1, 1, n, last + 1, bef[x] - 1);
      if (ans.first > bef[x]) {
        v.push_back(ans.second);
        v.push_back(x);
        v.push_back(ans.second);
        v.push_back(x);
        last = i;
      }
      update(1, 1, n, bef[x], make_pair(i, x));
    }
    bef[x] = i;
  }
  cout << v.size() << '\n';
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
    cout << *it << ' ';
  cout << '\n';
  return 0;
}
