#include <bits/stdc++.h>
using namespace std;
const int maxn = 500050;
int n, a[maxn], a0[maxn];
int ord[maxn];
int la[maxn], cnt[maxn], to[maxn];
int ans[maxn], anss = 0;
bool cmp(const int &xx, const int &yy) { return a[xx] < a[yy]; }
int main() {
  int i, j, k;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) ord[i] = i, scanf("%d", &a[i]);
  memcpy(a0, a, sizeof(a));
  sort(ord + 1, ord + 1 + n, cmp);
  for (i = k = 1; i <= n; i = j, k++) {
    a[ord[i]] = k;
    for (j = i + 1; a0[ord[j]] == a0[ord[j - 1]]; j++) a[ord[j]] = k;
  }
  for (i = 1; i <= n;) {
    for (j = i; j <= n; j++) {
      if (la[a[j]]) {
        if (to[la[a[j]]]) {
          ans[anss++] = a0[to[la[a[j]]]];
          ans[anss++] = a0[j];
          break;
        } else
          for (k = j - 1; k > la[a[j]]; k--) {
            if (to[k]) k = to[k];
            to[k] = la[a[j]];
          }
      }
      if (++cnt[a[j]] == 4) {
        ans[anss++] = a0[j], ans[anss++] = a0[j];
        break;
      }
      la[a[j]] = j;
    }
    for (; i <= j; i++) cnt[a[i]] = la[a[i]] = 0;
  }
  printf("%d\n", anss * 2);
  for (i = 0; i < anss; i += 2)
    printf("%d %d %d %d%c", ans[i], ans[i + 1], ans[i], ans[i + 1],
           i < anss - 1 ? ' ' : '\n');
  return 0;
}
