#include <bits/stdc++.h>
using namespace std;
const int mx = 5e5 + 10;
const int mxt = 4 * mx;
const int inf = 1e7;
map<int, int> fst, lst;
map<int, vector<int> > vs;
int d[mx];
int st[mxt];
int qry(int u, int lu, int ru, int f, int t) {
  if (lu > ru) return inf;
  if (ru < f || t < lu) return inf;
  if (f <= lu && ru <= t)
    return st[u];
  else {
    int mid = ((lu + ru) >> 1);
    int lft = qry(2 * u + 1, lu, mid, f, t);
    int rht = qry(2 * u + 2, mid + 1, ru, f, t);
    return min(lft, rht);
  }
}
void upd(int u, int lu, int ru, int idx, int v) {
  if (lu <= idx && idx <= ru) {
    if (lu == ru)
      st[u] = v;
    else {
      int mid = ((lu + ru) >> 1);
      upd(2 * u + 1, lu, mid, idx, v);
      upd(2 * u + 2, mid + 1, ru, idx, v);
      st[u] = min(st[2 * u + 1], st[2 * u + 2]);
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", d + i);
  fill(st, st + mxt, inf);
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    if (lst.count(d[i]) != 0) {
      int idx = lst[d[i]];
      int mini = qry(0, 0, n - 1, idx + 1, i - 1);
      vs[d[i]].push_back(i);
      lst[d[i]] = i;
      if (mini < idx) {
        ans.push_back(d[mini]);
        ans.push_back(d[i]);
        ans.push_back(d[mini]);
        ans.push_back(d[i]);
        fst.clear();
        lst.clear();
        vs.clear();
      } else if (vs[d[i]].size() >= 4) {
        ans.push_back(d[vs[d[i]][0]]);
        ans.push_back(d[vs[d[i]][1]]);
        ans.push_back(d[vs[d[i]][2]]);
        ans.push_back(d[vs[d[i]][3]]);
        fst.clear();
        lst.clear();
        vs.clear();
      }
      upd(0, 0, n - 1, i, fst[d[i]]);
    } else {
      fst[d[i]] = lst[d[i]] = i;
      vector<int> vv;
      vv.push_back(i);
      vs[d[i]] = vv;
    }
  }
  cout << ans.size() << '\n';
  for (int i = 0; i < int(ans.size()); i++) {
    if (i != 0) cout << " ";
    cout << ans[i];
  }
  cout << '\n';
  return 0;
}
