#include <bits/stdc++.h>
using namespace std;
struct node {
  int l, r;
  pair<int, int> mn = {1e9, 1e9};
};
const int MAXN = 5e5 + 10;
pair<int, pair<int, int> > ls[MAXN];
int n, ps[MAXN], dp[MAXN], a[MAXN];
vector<pair<int, int> > vc;
map<int, int> nxt, nxt2;
node vs[MAXN * 4];
void make(int v, int l, int r) {
  vs[v].l = l, vs[v].r = r;
  if (r - l == 1) return;
  int mid = (l + r) / 2;
  make(2 * v, l, mid);
  make(2 * v + 1, mid, r);
}
void add(int v, int ql, int qr, int val) {
  if (qr <= vs[v].l || vs[v].r <= ql) return;
  if (ql <= vs[v].l && vs[v].r <= qr) {
    vs[v].mn = min(vs[v].mn, {val, a[qr]});
    return;
  }
  add(2 * v, ql, qr, val);
  add(2 * v + 1, ql, qr, val);
}
pair<int, int> get(int v, int ind) {
  if (ind < vs[v].l || vs[v].r <= ind) return {1e9, 1e9};
  if (vs[v].l == ind && vs[v].r == vs[v].l + 1) return vs[v].mn;
  pair<int, int> r = min(get(2 * v, ind), get(2 * v + 1, ind));
  return min(r, vs[v].mn);
}
int main() {
  cin >> n;
  make(1, 0, n);
  for (int i = 0; i < n; i++) {
    ps[i] = 1e9;
    ls[i] = {-1, {-1, -1}};
    cin >> a[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    if (nxt[a[i]] != 0) {
      pair<int, int> p = get(1, nxt[a[i]]);
      if (p.first < n)
        ls[p.first] = max(ls[p.first], {i - 1, {a[i], p.second}});
      if (nxt2[a[i]] != 0) {
        p = get(1, nxt2[a[i]]);
        if (p.first < n)
          ls[p.first] = max(ls[p.first], {i - 1, {a[i], p.second}});
      }
      if (p.first < n)
        ls[p.first] = max(ls[p.first], {i - 1, {a[i], p.second}});
      if (i + 1 != nxt[a[i]]) add(1, i + 1, nxt[a[i]], nxt[a[i]]);
      if (nxt2[a[i]] != 0) add(1, i + 1, nxt2[a[i]], nxt2[a[i]]);
    }
    nxt2[a[i]] = nxt[a[i]];
    nxt[a[i]] = i;
  }
  for (int i = 1; i < n; i++) ls[i] = max(ls[i], ls[i - 1]);
  int cur = n - 1;
  while (cur != -1 && ls[cur].second.first != -1) {
    vc.push_back(ls[cur].second);
    cur = ls[cur].first;
  }
  cout << vc.size() * 4 << '\n';
  while (!vc.empty()) {
    cout << vc.back().first << ' ' << vc.back().second << ' ' << vc.back().first
         << ' ' << vc.back().second << ' ';
    vc.pop_back();
  }
}
