#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class T>
inline void scan_d(T &ret) {
  char c;
  int flag = 0;
  ret = 0;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  if (c == '-') {
    flag = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') ret = ret * 10 + (c - '0'), c = getchar();
  if (flag) ret = -ret;
}
const int maxn = 500000 + 10;
map<int, int> mp;
int a[maxn], pre[maxn];
struct node {
  node() {}
  node(int _l, int _r) : l(_l), r(_r) {}
  int l, r;
  bool operator<(const node &t) const { return l != t.l ? l < t.l : r > t.r; }
} no[maxn * 2];
int T[maxn * 4];
void build(int l, int r, int rt) {
  T[rt] = 0;
  if (l == r) return;
  int mid = l + r >> 1;
  build(l, mid, rt << 1);
  build(mid + 1, r, rt << 1 | 1);
}
void update(int L, int x, int l, int r, int rt) {
  if (l == r) {
    T[rt] = max(T[rt], x);
    return;
  }
  int mid = l + r >> 1;
  if (L <= mid)
    update(L, x, l, mid, rt << 1);
  else
    update(L, x, mid + 1, r, rt << 1 | 1);
  T[rt] = max(T[rt << 1], T[rt << 1 | 1]);
}
int query(int L, int R, int l, int r, int rt) {
  if (L <= l && R >= r) return T[rt];
  int mid = l + r >> 1, x = 0;
  if (L <= mid) x = max(x, query(L, R, l, mid, rt << 1));
  if (R > mid) x = max(x, query(L, R, mid + 1, r, rt << 1 | 1));
  return x;
}
int data[maxn];
int dp[maxn][2];
stack<int> st;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (mp[a[i]])
      pre[i] = mp[a[i]];
    else
      pre[i] = 0;
    mp[a[i]] = i;
    data[i] = -1;
  }
  int nct = 0;
  for (int i = 1; i <= n; i++) {
    if (pre[i]) {
      no[nct++] = node(pre[i], i);
      int x = pre[pre[i]];
      if (x) no[nct++] = node(x, i);
    }
  }
  sort(no, no + nct);
  build(1, n, 1);
  for (int i = 0; i < nct; i++) {
    if (no[i].l + 1 < no[i].r) {
      int x = query(no[i].l + 1, no[i].r - 1, 1, n, 1);
      if (x) data[no[i].r] = max(data[no[i].r], x);
    }
    update(no[i].r, no[i].l, 1, n, 1);
  }
  for (int i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0];
    dp[i][1] = dp[i - 1][1];
    if (data[i] != -1) {
      if (dp[i][0] < dp[data[i] - 1][0] + 1) {
        dp[i][0] = dp[data[i] - 1][0] + 1;
        dp[i][1] = i;
      }
    }
  }
  printf("%d\n", dp[n][0] * 4);
  int x = dp[n][1];
  while (x) {
    st.push(a[x]);
    st.push(a[data[x]]);
    st.push(a[x]);
    st.push(a[data[x]]);
    x = dp[data[x] - 1][1];
  }
  while (!st.empty()) {
    int x = st.top();
    st.pop();
    printf("%d%c", x, st.empty() ? '\n' : ' ');
  }
  return 0;
}
