#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 19;
int n, a[N], last[N], pre[N], b[N];
pair<int, int> v[N];
vector<int> V[N];
void input() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    b[i] = a[i];
  }
}
void nen() {
  for (int i = 1; i <= n; i++) v[i] = pair<int, int>(a[i], i);
  sort(v + 1, v + n + 1);
  a[v[1].second] = 1;
  for (int i = 2; i <= n; i++)
    if (v[i].first != v[i - 1].first)
      a[v[i].second] = a[v[i - 1].second] + 1;
    else
      a[v[i].second] = a[v[i - 1].second];
  for (int i = 1; i <= n; i++) {
  }
}
int f[N], tree[N << 2], nex[N];
void update(int k, int p, int q, int x, int v) {
  if (p == q) {
    tree[k] = v;
    return;
  }
  int m = p + q >> 1;
  if (x <= m)
    update(2 * k, p, m, x, v);
  else
    update(2 * k + 1, m + 1, q, x, v);
  tree[k] = max(tree[2 * k], tree[2 * k + 1]);
}
int xxx;
void Left(int k, int p, int q, int x, int y, int v) {
  if (p > y || q < x || p > q) return;
  if (xxx) return;
  if (p >= x && q <= y) {
    if (tree[k] <= v) return;
    if (p == q) {
      xxx = p;
      return;
    }
    if (tree[2 * k + 1] > v)
      Left(2 * k + 1, (p + q >> 1) + 1, q, x, y, v);
    else
      Left(2 * k, p, p + q >> 1, x, y, v);
  }
  Left(2 * k + 1, (p + q >> 1) + 1, q, x, y, v);
  Left(2 * k, p, p + q >> 1, x, y, v);
}
vector<int> vi;
void process() {
  for (int i = 1; i <= n; i++) {
    f[i] = f[i - 1];
    pre[i] = last[a[i]];
    last[a[i]] = i;
    V[a[i]].push_back(i);
    nex[pre[i]] = i;
    if (!pre[i]) continue;
    if (V[a[i]].size() >= 4)
      f[i] = max(f[i], f[V[a[i]][V[a[i]].size() - 4] - 1] + 1);
    xxx = 0;
    Left(1, 1, n, 1, pre[i] - 1, pre[i]);
    if (xxx) f[i] = max(f[i], f[xxx - 1] + 1);
    update(1, 1, n, pre[i], i);
    if (f[i] > f[i - 1]) {
      if (xxx && f[i] == f[xxx - 1] + 1) {
        vi.push_back(b[xxx]);
        vi.push_back(b[pre[i]]);
        vi.push_back(b[nex[xxx]]);
        vi.push_back(b[i]);
      } else {
        vi.push_back(b[i]);
        vi.push_back(b[i]);
        vi.push_back(b[i]);
        vi.push_back(b[i]);
      }
    }
  }
  printf("%d\n", int(vi.size()));
  for (int i = 0; i < vi.size(); i++) printf("%d ", vi[i]);
}
int main() {
  input();
  nen();
  process();
  return 0;
}
