#include <bits/stdc++.h>
using namespace std;
map<int, int> pre, nb;
int top, s[500100], x, n, sol[500100], t;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    if (pre[x]) {
      for (register int j = 0; j < 2; ++j) {
        sol[++t] = pre[x];
        sol[++t] = x;
      }
      pre.clear();
      nb.clear();
      top = 0;
      continue;
    }
    while (top && (nb[x] > 1 || (nb[x] == 1 && x != s[top]))) {
      pre[s[top]] = x;
      nb[s[top]]--;
      top--;
    }
    s[++top] = x;
    nb[x]++;
  }
  cout << t << "\n";
  for (int i = 1; i <= t; ++i) cout << sol[i] << " ";
  return 0;
}
