#include <bits/stdc++.h>
using namespace std;
const int oo = 1e9 + 1;
const int N = 5e5 + 1;
int n, a[N], last[2][N], match[2][N], seg[N << 2], dp[N], par[N];
map<int, pair<int, int>> m;
vector<pair<pair<int, int>, int>> q;
pair<int, int> p[N];
vector<int> ans;
void add(int ind, int id = 1, int l = 1, int r = n) {
  if (l == r) {
    seg[id] = last[1][l];
    return;
  }
  int mid = l + r >> 1;
  if (ind <= mid)
    add(ind, id << 1, l, mid);
  else
    add(ind, id << 1 | 1, mid + 1, r);
  seg[id] = max(seg[id << 1], seg[id << 1 | 1]);
}
int get(int L, int R, int id = 1, int l = 1, int r = n) {
  if (R < l || r < L) return -1;
  if (L <= l && r <= R) return seg[id];
  int mid = l + r >> 1;
  return max(get(L, R, id << 1, l, mid), get(L, R, id << 1 | 1, mid + 1, r));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    if (!m.count(a[i])) m[a[i]] = {oo, oo};
    last[0][i] = m[a[i]].first;
    last[1][i] = m[a[i]].second;
    if (last[0][i] != oo) q.push_back({{last[0][i] + 1, i - 1}, -i});
    if (last[1][i] != oo) q.push_back({{last[1][i] + 1, i - 1}, i});
    m[a[i]].first = m[a[i]].second;
    m[a[i]].second = i;
  }
  sort(q.begin(), q.end());
  for (int i = 1; i <= n; i++) p[i - 1] = {last[1][i], i};
  sort(p, p + n);
  memset(match, -1, sizeof match);
  memset(seg, -1, sizeof seg);
  for (int i = 0, j = 0; i < q.size(); i++) {
    int ind = q[i].first.first - 2;
    while (j < n && p[j].first <= ind) {
      add(p[j].second);
      j++;
    }
    int maxi = get(q[i].first.first, q[i].first.second);
    if (q[i].second < 0)
      match[0][-q[i].second] = maxi;
    else
      match[1][q[i].second] = maxi;
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    for (int j = 0; j < 2; j++)
      if (match[j][i] != -1 && dp[i] < dp[match[j][i] - 1] + 1) {
        par[i] = j + 1;
        dp[i] = dp[match[j][i] - 1] + 1;
      }
    if (last[0][i] != oo && last[1][last[0][i]] != oo)
      if (dp[i] < dp[last[1][last[0][i]] - 1] + 1) {
        dp[i] = dp[last[1][last[0][i]] - 1] + 1;
        par[i] = 3;
      }
  }
  for (int i = n; i > 0;) {
    for (int j = 0; j < 2; j++)
      if (par[i] == j + 1) {
        for (int k = 0; k < 2; k++) {
          ans.push_back(a[i]);
          ans.push_back(a[match[j][i]]);
        }
        i = last[j][i];
      }
    if (par[i] == 3) {
      for (int k = 0; k < 4; k++) ans.push_back(a[i]);
      i = last[1][last[0][i]];
    }
    i--;
  }
  printf("%d\n", ans.size());
  reverse(ans.begin(), ans.end());
  for (auto x : ans) printf("%d ", x);
  printf("\n");
}
