#include <bits/stdc++.h>
using namespace std;
const int N = 500005, INF = 0x3f3f3f3f;
int n, a[N], b[N], d[N], val[N << 2], L, tot, ans[N << 2];
map<int, int> M;
void push_up(int rt) { val[rt] = min(val[rt << 1], val[rt << 1 | 1]); }
void build(int l = 1, int r = n, int rt = 1) {
  if (l == r) {
    val[rt] = b[l];
    return;
  }
  int m = l + r >> 1;
  build(l, m, rt << 1);
  build(m + 1, r, rt << 1 | 1);
  push_up(rt);
}
int query(int L, int R, int l = 1, int r = n, int rt = 1) {
  if (L <= l && r <= R) {
    return val[rt];
  }
  int m = l + r >> 1;
  int ret = INF;
  if (L <= m) ret = min(ret, query(L, R, l, m, rt << 1));
  if (R > m) ret = min(ret, query(L, R, m + 1, r, rt << 1 | 1));
  return ret;
}
void update(int p, int l = 1, int r = n, int rt = 1) {
  if (l == r) {
    val[rt] = b[p] = p;
    return;
  }
  int m = l + r >> 1;
  if (p <= m)
    update(p, l, m, rt << 1);
  else
    update(p, m + 1, r, rt << 1 | 1);
  push_up(rt);
}
int work(int i) {
  int j = b[i];
  if (j + 1 <= i - 1) {
    int ret = query(j + 1, i - 1);
    if (ret < j) {
      ans[tot++] = a[ret];
      ans[tot++] = a[i];
      ans[tot++] = a[ret];
      ans[tot++] = a[i];
      return 1;
    }
  }
  if (j != i) {
    int k = b[j];
    if (k + 1 <= i - 1) {
      int ret = query(k + 1, i - 1);
      if (ret < k) {
        ans[tot++] = a[ret];
        ans[tot++] = a[i];
        ans[tot++] = a[ret];
        ans[tot++] = a[i];
        return 1;
      }
    }
    if (k != j) {
      int t = b[k];
      if (t != k) {
        ans[tot++] = a[i];
        ans[tot++] = a[i];
        ans[tot++] = a[i];
        ans[tot++] = a[i];
        return 1;
      }
    }
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    if (M.count(a[i]) == 0) {
      b[i] = i;
    } else {
      d[M[a[i]]] = i;
      b[i] = M[a[i]];
    }
    d[i] = i;
    M[a[i]] = i;
  }
  build();
  L = 1;
  for (int i = 1; i <= n; ++i) {
    if (work(i)) {
      for (int j = L; j <= i; ++j) {
        update(d[j]);
      }
      L = i + 1;
    }
  }
  printf("%d\n", tot);
  for (int i = 0; i < tot; ++i) printf("%d ", ans[i]);
  printf("\n");
  return 0;
}
