#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
void Open() {}
const int N = 500050;
int a[N];
int sta[N];
vector<int> v[N];
int idx[N];
int pid[N];
int L[N];
int dp[N];
int pe[N];
int ans[N];
struct node {
  int l, r, ma;
  bool operator<(const node& o) const {
    return l < o.l || (l == o.l && r < o.r);
  }
} lt[N * 5];
void push_up(int x) { lt[x].ma = max(lt[x << 1].ma, lt[x << 1 | 1].ma); }
void build(int l, int r, int x) {
  lt[x] = (node){l, r, -1};
  if (l == r) return;
  int mid = l + r >> 1;
  build(l, mid, x << 1);
  build(mid + 1, r, x << 1 | 1);
}
void update(int idx, int v, int x) {
  if (idx == lt[x].l && idx == lt[x].r) {
    lt[x].ma = max(lt[x].ma, v);
    return;
  }
  int mid = lt[x].l + lt[x].r >> 1;
  if (idx <= mid)
    update(idx, v, x << 1);
  else
    update(idx, v, x << 1 | 1);
  push_up(x);
}
int getma(int l, int r, int x) {
  if (l > r) return -1;
  if (l <= lt[x].l && lt[x].r <= r) {
    return lt[x].ma;
  }
  int mid = lt[x].l + lt[x].r >> 1;
  if (r <= mid)
    return getma(l, r, x << 1);
  else if (l > mid)
    return getma(l, r, x << 1 | 1);
  else
    return max(getma(l, mid, x << 1), getma(mid + 1, r, x << 1 | 1));
}
vector<node> que;
int main() {
  memset(pe, -1, sizeof(pe));
  memset(L, -1, sizeof(L));
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sta[i] = a[i];
  }
  sort(sta, sta + n);
  int t = unique(sta, sta + n) - sta;
  for (int i = 0; i < n; i++) a[i] = lower_bound(sta, sta + t, a[i]) - sta;
  build(0, n - 1, 1);
  memset(idx, -1, sizeof(idx));
  for (int i = 0; i < n; i++) {
    pid[i] = idx[a[i]];
    if (pid[i] != -1) que.push_back((node){pid[pid[i]], i});
    idx[a[i]] = i;
    que.push_back((node){pid[i], i});
    v[a[i]].push_back(i);
  }
  sort(que.begin(), que.end());
  int j = 0;
  for (int i = 0; i < que.size(); i++) {
    while (j < que.size() && que[j].l < que[i].l) {
      if (que[j].l >= 0) update(que[j].r, que[j].l, 1);
      j++;
    }
    int& res = L[que[i].r];
    res = max(res, getma(que[i].l + 1, que[i].r - 1, 1));
  }
  int st = -1;
  for (int i = 0; i < n; i++) {
    if (i < 3) {
      dp[i] = 0;
      continue;
    }
    if (L[i] == -1)
      dp[i] = dp[i - 1];
    else if (L[i] == 0)
      dp[i] = 1, pe[i] = L[i];
    else {
      dp[i] = dp[L[i] - 1] + 1, pe[i] = L[i];
      if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1], pe[i] = -1;
    }
  }
  printf("%d\n", dp[n - 1] * 4);
  int res = dp[n - 1];
  int tmp = n - 1;
  while (tmp >= 0) {
    if (pe[tmp] == -1) {
      tmp--;
      continue;
    } else if (dp[tmp] == res) {
      res--;
      ans[res] = tmp;
      tmp = pe[tmp] - 1;
    } else {
      tmp--;
    }
  }
  for (int i = 0; i < dp[n - 1]; i++) {
    int u = ans[i];
    int v = pe[u];
    u = sta[a[u]], v = sta[a[v]];
    printf("%d %d %d %d ", v, u, v, u);
  }
  printf("\n");
  return 0;
}
