#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, N = 5e5 + 7;
struct RMQ {
  vector<int> t;
  int n;
  RMQ(int n) : n(n) { t.resize(n << 1); }
  void build(int a[]) {
    for (int i = 0; i < n; i++) t[n + i] = a[i];
    for (int i = n - 1; i > 0; --i) t[i] = min(t[i << 1], t[i << 1 | 1]);
  }
  void modify(int p, int v) {
    for (t[p += n] = v; p > 1; p >>= 1) t[p >> 1] = min(t[p], t[p ^ 1]);
  }
  int query(int l, int r) {
    int res = inf;
    for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) res = min(res, t[l++]);
      if (r & 1) res = min(res, t[--r]);
    }
    return res;
  }
};
int a[N], nxt[N], prv[N], go[N], dp[N], n;
int g(int x) {
  x = nxt[x];
  if (x > n) {
    return x;
  }
  x = nxt[x];
  if (x > n) {
    return x;
  }
  x = nxt[x];
  return x;
}
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    prv[i] = mp[a[i]];
    mp[a[i]] = i;
  }
  mp.clear();
  for (int i = n; i >= 1; i--) {
    if (!mp.count(a[i])) {
      nxt[i] = inf;
    } else {
      nxt[i] = mp[a[i]];
    }
    mp[a[i]] = i;
  }
  RMQ SegTree(n + 2);
  for (int i = 1; i <= n; i++) {
    if (prv[i]) {
      go[prv[i]] = SegTree.query(prv[i] + 1, i - 1);
      SegTree.modify(prv[i], inf);
    }
    SegTree.modify(i, nxt[i]);
  }
  for (int i = n; i >= 1; i--) {
    dp[i] = dp[i + 1];
    if (go[i] && go[i] <= n) {
      dp[i] = max(dp[i], 4 + dp[go[i] + 1]);
    }
    int x = g(i);
    if (x <= n) {
      dp[i] = max(dp[i], 4 + dp[x + 1]);
    }
  }
  cout << dp[1] << endl;
  int ptr = 1;
  while (ptr <= n) {
    if (dp[ptr] == dp[ptr + 1]) {
      ++ptr;
    } else if (go[ptr] && go[ptr] <= n && dp[ptr] == 4 + dp[go[ptr] + 1]) {
      cout << a[ptr] << " " << a[go[ptr]] << " " << a[ptr] << " " << a[go[ptr]]
           << " ";
      ptr = go[ptr] + 1;
    } else {
      int x = g(ptr);
      cout << a[ptr] << " " << a[ptr] << " " << a[ptr] << " " << a[ptr] << " ";
      ptr = x + 1;
    }
  }
}
