#include <bits/stdc++.h>
using namespace std;
int const M = 5e5 + 10, inf = 1e9 + 10;
int dp[M], seg[M * 4], a[M], id[M * 4], good[M];
map<int, int> last;
map<int, int> has;
int nxt[M];
int minn = inf, ind;
vector<int> nu[M];
vector<int> q[M];
void update(int l, int r, int inn, int x, int node) {
  int mid = (l + r) / 2;
  if (r - l == 1) {
    seg[node] = x;
    id[node] = l;
    return;
  }
  if (inn < mid)
    update(l, mid, inn, x, node * 2);
  else
    update(mid, r, inn, x, node * 2 + 1);
  if (seg[node * 2] < seg[node * 2 + 1])
    id[node] = id[node * 2];
  else
    id[node] = id[node * 2 + 1];
  seg[node] = min(seg[node * 2], seg[node * 2 + 1]);
}
void get(int l, int r, int st, int en, int node) {
  int mid = (l + r) / 2;
  if (st <= l && r <= en) {
    if (seg[node] < minn) {
      minn = seg[node];
      ind = id[node];
    }
    return;
  }
  if (st >= r || l >= en) return;
  get(l, mid, st, en, node * 2);
  get(mid, r, st, en, node * 2 + 1);
}
int main() {
  int n, cnt = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = n; i >= 1; i--) {
    if (last[a[i]])
      nxt[i] = last[a[i]];
    else
      nxt[i] = inf;
    if (!has[a[i]]) has[a[i]] = ++cnt;
    nu[has[a[i]]].push_back(i);
    int tmp = has[a[i]];
    if (nu[tmp].size() >= 4) good[i] = nu[tmp][(int)nu[tmp].size() - 3];
    last[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) last[a[i]] = 0;
  for (int i = 1; i < 4 * M; i++) seg[i] = inf;
  for (int i = n; i >= 1; i--) {
    minn = inf + 1, ind = 0;
    if (!good[i]) good[i] = inf;
    if (nxt[i] == inf || nxt[i] == i + 1) continue;
    if (nxt[ind] == inf) continue;
    q[nxt[i] - 1].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (last[a[i]]) update(1, n + 1, last[a[i]], inf, 1);
    update(1, n + 1, i, nxt[i], 1);
    last[a[i]] = i;
    for (int j = 0; j < q[i].size(); j++) {
      minn = inf;
      get(1, n + 1, q[i][j] + 1, i + 1, 1);
      if (minn != inf) {
        int tmp = q[i][j];
        if (good[tmp] == inf) {
          good[tmp] = ind;
          continue;
        }
        if (nxt[ind] < nxt[good[tmp]]) good[tmp] = ind;
      }
    }
  }
  for (int i = n; i >= 1; i--) {
    dp[i] = dp[i + 1];
    if (good[i] == inf || nxt[good[i]] == inf) continue;
    if (dp[i] > dp[nxt[good[i]] + 1] + 1) {
      good[i] = inf;
      continue;
    }
    dp[i] = dp[nxt[good[i]] + 1] + 1;
  }
  int now = 1;
  cout << dp[1] * 4 << "\n";
  while (true) {
    if (dp[now] == 0) break;
    if (good[now] == inf) {
      now++;
      continue;
    }
    printf("%d %d %d %d ", a[now], a[good[now]], a[now], a[good[now]]);
    now = nxt[good[now]] + 1;
  }
}
