#include <bits/stdc++.h>
using namespace std;
int a[500005], nxt[500005], Lst[500005], r[500005], dp[500005], lst[500005];
map<int, int> mp;
struct stree {
  int l, r, mn;
} t[2000005];
inline void build(int now, int l, int r) {
  t[now].l = l, t[now].r = r;
  if (l == r) {
    t[now].mn = nxt[l];
    return;
  }
  int mid = (l + r) / 2;
  build(now * 2, l, mid), build(now * 2 + 1, mid + 1, r);
  t[now].mn = min(t[now * 2].mn, t[now * 2 + 1].mn);
}
inline void change(int now, int x, int d) {
  if (t[now].l == t[now].r) {
    t[now].mn = d;
    return;
  }
  if (x <= t[now * 2].r)
    change(now * 2, x, d);
  else
    change(now * 2 + 1, x, d);
  t[now].mn = min(t[now * 2].mn, t[now * 2 + 1].mn);
}
inline int query(int now, int l, int r) {
  if (t[now].l == l && t[now].r == r) return t[now].mn;
  if (t[now * 2].r >= r)
    return query(now * 2, l, r);
  else if (t[now * 2 + 1].l <= l)
    return query(now * 2 + 1, l, r);
  else
    return min(query(now * 2, l, t[now * 2].r),
               query(now * 2 + 1, t[now * 2 + 1].l, r));
}
vector<int> v;
int main(int argc, char** argv) {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    int qwq = mp[a[i]];
    nxt[i] = n + 1, r[i] = n + 1, nxt[qwq] = i, lst[i] = qwq, mp[a[i]] = i;
  }
  build(1, 1, n);
  for (int i = 1; i <= n; i++)
    if (lst[i]) change(1, lst[i], n + 1), r[lst[i]] = query(1, lst[i], i - 1);
  for (int i = 1; i <= n + 1; i++) {
    dp[i] = max(dp[i], dp[i - 1]), dp[r[i] + 1] = max(dp[r[i] + 1], dp[i] + 4);
    if (lst[lst[lst[i]]]) dp[i + 1] = max(dp[i + 1], dp[lst[lst[lst[i]]]] + 4);
    if (dp[i - 1] == dp[i]) Lst[i] = Lst[i - 1];
    if (lst[lst[lst[i]]] && dp[i + 1] == dp[lst[lst[lst[i]]]] + 4)
      Lst[i + 1] = lst[lst[lst[i]]];
    if (r[i])
      if (dp[r[i] + 1] == dp[i] + 4) Lst[r[i] + 1] = i;
  }
  cout << dp[n + 1] << "\n";
  int now = n + 1;
  while (now >= 1) {
    v.push_back(now);
    now = Lst[now];
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size() - 1; i++) {
    int t = v[i];
    cout << a[t] << " ";
    if (nxt[nxt[nxt[t]]] < v[i + 1] && nxt[nxt[nxt[t]]]) {
      cout << a[t] << " ";
      cout << a[t] << " ";
      cout << a[t] << " ";
      continue;
    }
    for (int j = t + 1; j < nxt[t]; j++) {
      if (nxt[j] <= n && nxt[j] > nxt[t] && nxt[j] < v[i + 1]) {
        cout << a[j] << " " << a[nxt[t]] << " " << a[nxt[j]] << " ";
        break;
      }
    }
  }
  return 0;
}
