#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
long long f[maxn], a[maxn];
int n;
map<long long, long long> pre, sum;
vector<long long> ans;
void fir() {
  int st = 1;
  while (st <= n) {
    sum.clear();
    pre.clear();
    stack<long long> s;
    for (int& i = st; i <= n; i++) {
      if (pre[a[i]]) {
        ans.push_back(pre[a[i]]);
        ans.push_back(a[i]);
        ans.push_back(pre[a[i]]);
        ans.push_back(a[i]);
        break;
      }
      while (!s.empty() &&
             (sum[a[i]] > 1 || (sum[a[i]] == 1 && s.top() != a[i]))) {
        pre[s.top()] = a[i];
        sum[s.top()]--;
        s.pop();
      }
      s.push(a[i]);
      sum[a[i]]++;
    }
    st++;
  }
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); i++) {
    printf("%I64d", ans[i]);
    if (i == ans.size() - 1) {
      printf("\n");
    } else
      printf(" ");
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
  }
  fir();
}
