#include <bits/stdc++.h>
using namespace std;
template <class P, class Q>
inline void smin(P &a, Q b) {
  if (b < a) a = b;
}
template <class P, class Q>
inline void smax(P &a, Q b) {
  if (a < b) a = b;
}
const int maxn = 500000 + 100;
int n;
int a[maxn], b[maxn], bs[maxn];
vector<int> c[maxn];
int id[maxn];
set<int> s;
vector<int> ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0, _n = (int)(n); i < _n; i++) cin >> a[i];
  copy(a, a + n, b);
  copy(a, a + n, bs);
  sort(bs, bs + n);
  int nn = unique(bs, bs + n) - bs;
  for (int i = 0, _n = (int)(n); i < _n; i++)
    b[i] = lower_bound(bs, bs + nn, b[i]) - bs;
  for (int i = 0, _n = (int)(n); i < _n; i++)
    id[i] = (int((c[b[i]]).size())), c[b[i]].push_back(i);
  int e = n, op = -1;
  for (int i = 0, _n = (int)(n); i < _n; i++) {
    if (i == e) {
      ans.push_back(a[op]);
      ans.push_back(a[i]);
      ans.push_back(a[op]);
      ans.push_back(a[i]);
      s.clear();
      e = n;
      continue;
    }
    vector<int> &v = c[b[i]];
    if (id[i] + 1 == (int((v).size()))) continue;
    while ((int((s).size())) && *s.begin() <= i) {
      int t = *s.begin();
      s.erase(s.begin());
      if (id[t] + 1 < (int((c[b[t]]).size()))) s.insert(c[b[t]][id[t] + 1]);
    }
    int p = -1;
    if ((int((s).size()))) p = *s.begin();
    s.insert(v[id[i] + 1]);
    if (p == -1) continue;
    int ed = upper_bound((v).begin(), (v).end(), p) - v.begin();
    if (ed != (int((v).size()))) {
      ed = v[ed];
      smin(e, ed);
      if (e == ed) op = p;
    }
  }
  cout << (int((ans).size())) << endl;
  for (int i = 0, _n = (int)((int((ans).size()))); i < _n; i++)
    cout << ans[i] << ' ';
  cout << endl;
  return 0;
}
