#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
int a[(1000000 + 10)];
int san[(1000000 + 10)], c;
int que[(1000000 + 10)], head, tail;
int cnt[(1000000 + 10)], nxt[(1000000 + 10)];
int ans[(1000000 + 10)], ret;
int tmp[(1000000 + 10)], tot;
int hx(int x) { return lower_bound(san, san + c, x) - san; }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), san[c++] = a[i];
  sort(san, san + c);
  c = unique(san, san + c) - san;
  for (int i = 1; i <= n; ++i) a[i] = hx(a[i]);
  memset(nxt, -1, sizeof nxt);
  for (int i = 1; i <= n; ++i) {
    if (nxt[a[i]] != -1) {
      ans[ret++] = san[nxt[a[i]]], ans[ret++] = san[a[i]];
      ans[ret++] = san[nxt[a[i]]], ans[ret++] = san[a[i]];
      while (tail) {
        --tail;
        --cnt[que[tail]];
      }
      while (tot) {
        --tot;
        nxt[tmp[tot]] = -1;
      }
    } else {
      if (cnt[a[i]]) {
        while (head < tail && (que[tail - 1] != a[i] || cnt[a[i]] > 1)) {
          --tail;
          nxt[que[tail]] = a[i];
          tmp[tot++] = que[tail];
          --cnt[que[tail]];
        }
      }
      cnt[a[i]]++;
      que[tail++] = a[i];
    }
  }
  printf("%d\n", ret);
  for (int i = 0; i < ret; ++i)
    printf("%d%c", ans[i], i == ret - 1 ? '\n' : ' ');
}
