#include <bits/stdc++.h>
using namespace std;
const int N = 5 * 1e5 + 5;
map<int, int> mp;
struct node {
  int l, r;
  bool operator<(const node& b) const { return l < b.l; }
};
vector<node> v;
node x;
int L[N], mx[N], dp[N], ans, pre[N], fst, a[N], n, f[N][5];
struct Stree {
  int mx[4 * N], L, R, pos, val;
  void init() { memset(mx, 0, sizeof(mx)); }
  void insert(int o, int l, int r) {
    if (l == r) {
      mx[o] = val;
      return;
    }
    if (pos <= ((l + r) >> 1))
      insert((o << 1), l, ((l + r) >> 1));
    else
      insert(((o << 1) | 1), ((l + r) >> 1) + 1, r);
    mx[o] = max(mx[(o << 1)], mx[((o << 1) | 1)]);
  }
  int get_mx(int o, int l, int r) {
    int ret = 0;
    if (L <= l && r <= R) return mx[o];
    if (L <= ((l + r) >> 1)) ret = get_mx((o << 1), l, ((l + r) >> 1));
    if (R > ((l + r) >> 1))
      ret = max(ret, get_mx(((o << 1) | 1), ((l + r) >> 1) + 1, r));
    return ret;
  }
} S;
void print(int x) {
  if (!x) return;
  print(pre[x]);
  if (fst++) printf(" ");
  printf("%d %d %d %d", a[L[x]], a[x], a[L[x]], a[x]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    if (mp.count(a[i])) {
      v.push_back((node){mp[a[i]], i});
      f[i][1] = mp[a[i]];
    } else
      f[i][1] = 0;
    mp[a[i]] = i;
  }
  sort(v.begin(), v.end());
  S.init();
  for (int i = 0; i < v.size(); i++) {
    x = v[i];
    if (x.l + 1 == x.r) continue;
    S.L = x.l + 1;
    S.R = x.r - 1;
    L[x.r] = S.get_mx(1, 1, n);
    S.pos = x.r;
    S.val = x.l;
    S.insert(1, 1, n);
  }
  for (int j = 2; j <= 3; j++) {
    for (int i = 1; i <= n; i++) {
      if (!f[i][j - 1]) continue;
      f[i][j] = f[f[i][j - 1]][1];
    }
  }
  for (int i = 1; i <= n; i++) L[i] = max(L[i], f[i][3]);
  mx[0] = dp[0] = 0;
  int ed = 0;
  for (int i = 1; i <= n; i++) {
    if (!L[i])
      dp[i] = 0;
    else {
      dp[i] = dp[mx[L[i] - 1]] + 1;
      pre[i] = mx[L[i] - 1];
    }
    mx[i] = mx[i - 1];
    if (dp[i] > dp[mx[i]]) mx[i] = i;
    if (dp[i] > dp[ed]) ed = i;
  }
  printf("%d\n", dp[ed] * 4);
  fst = 0;
  if (ed) {
    print(ed);
    puts("");
  }
  return 0;
}
