#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * power(a * a % md, b / 2, md) % md
                      : power(a * a % md, b / 2, md) % md));
}
const int xn = 5e5 + 10;
const int xm = -20;
const int sq = 320;
const int inf = 1e9 + 10;
const long long INF = 1e18 + 10;
const int mod = 1e9 + 7;
const int base = 257;
int n, a[xn], f[xn], dp[xn], seg[xn << 2], last[xn];
bool par[xn];
unordered_map<int, int> mp;
vector<int> query[xn], ans;
void modify(int id, int l, int r, int pos, int val) {
  seg[id] = val;
  if (r - l == 1) return;
  int mid = l + r >> 1;
  if (pos < mid)
    modify(id << 1, l, mid, pos, val);
  else
    modify(id << 1 | 1, mid, r, pos, val);
}
int get(int id, int l, int r, int ql, int qr) {
  if (qr <= l || r <= ql) return 0;
  if (ql <= l && r <= qr) return seg[id];
  int mid = l + r >> 1;
  return max(get(id << 1, l, mid, ql, qr), get(id << 1 | 1, mid, r, ql, qr));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    last[i] = mp[a[i]];
    mp[a[i]] = i;
    f[i] = last[last[last[i]]];
    query[last[i]].push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    for (int x : query[i]) f[x] = max(f[x], get(1, 1, n + 1, i + 1, x));
    for (int x : query[i]) modify(1, 1, n + 1, x, i);
  }
  for (int i = 1; i <= n; ++i) {
    dp[i] = dp[i - 1];
    par[i] = (f[i] && dp[f[i] - 1] + 1 > dp[i]);
    if (par[i]) dp[i] = dp[f[i] - 1] + 1;
  }
  int res = n;
  while (res) {
    if (par[res]) {
      ans.push_back(a[res]);
      ans.push_back(a[f[res]]);
      ans.push_back(a[res]);
      ans.push_back(a[f[res]]);
      res = f[res] - 1;
    } else
      --res;
  }
  reverse(ans.begin(), ans.end());
  cout << int(ans.size()) << '\n';
  for (int x : ans) cout << x << ' ';
  cout << '\n';
  return 0;
}
