#include <bits/stdc++.h>
using namespace std;
int n;
int a[(500500)];
vector<int> ans;
int Find(int SSS) {
  stack<int> S;
  map<int, int> sum, belong;
  for (int i = SSS; i <= n; i++) {
    if (belong[a[i]]) {
      ans.push_back(belong[a[i]]);
      ans.push_back(a[i]);
      ans.push_back(belong[a[i]]);
      ans.push_back(a[i]);
      return i;
    }
    while (S.size() && (sum[a[i]] > 1 || sum[a[i]] == 1 && S.top() != a[i])) {
      belong[S.top()] = a[i];
      sum[S.top()]--;
      S.pop();
    }
    S.push(a[i]);
    sum[a[i]]++;
  }
  return -1;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int pos(0);
  while ((pos = Find(pos + 1)) != -1)
    ;
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
  cout << endl;
  return 0;
}
