#include <bits/stdc++.h>
using namespace std;
auto rnd = bind(uniform_int_distribution<long long>(1, 1e18), mt19937(time(0)));
const int N = 5e5 + 1;
const int inf = 2e9;
int nxt[N];
pair<int, int> t[4 * N];
int a[N];
void upd(int id, pair<int, int> x, int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = x;
    return;
  }
  int tm = (tl + tr) >> 1;
  if (tm >= id)
    upd(id, x, 2 * v, tl, tm);
  else
    upd(id, x, 2 * v + 1, tm + 1, tr);
  t[v] = min(t[2 * v], t[2 * v + 1]);
}
pair<int, int> get(int l, int r, int v, int tl, int tr) {
  if (tl >= l && tr <= r) {
    return t[v];
  }
  if (tl > r || tr < l) return make_pair(inf, inf);
  int tm = (tl + tr) >> 1;
  return min(get(l, r, 2 * v, tl, tm), get(l, r, 2 * v + 1, tm + 1, tr));
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> kek;
  for (auto &z : x) cin >> z, kek.push_back(z);
  sort(kek.begin(), kek.end());
  kek.erase(unique(kek.begin(), kek.end()), kek.end());
  for (auto &z : x) z = lower_bound(kek.begin(), kek.end(), z) - kek.begin();
  vector<int> lst(n, 2e9);
  vector<array<int, 3>> go(n);
  for (int i = 0; i < n; i++) go[i][0] = go[i][1] = go[i][2] = inf;
  vector<vector<int>> values(n, vector<int>());
  for (int i = n - 1; i >= 0; i--) {
    values[x[i]].push_back(i);
    if ((int)values[x[i]].size() >= 4) {
      go[i][0] = values[x[i]][(int)values[x[i]].size() - 4];
      go[i][1] = x[i];
      go[i][2] = x[i];
    }
    a[i] = lst[x[i]];
    lst[x[i]] = i;
  }
  fill(t, t + 4 * N, make_pair(inf, inf));
  fill(lst.begin(), lst.end(), -1);
  vector<vector<int>> qry(n, vector<int>());
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] != 2e9 && a[i] > i) {
      qry[a[i] - 1].push_back(i);
    }
  }
  for (int i = 0; i < n; i++) {
    if (lst[x[i]] != -1) {
      upd(lst[x[i]], make_pair(2e9, 2e9), 1, 0, n - 1);
    }
    lst[x[i]] = i;
    upd(i, make_pair(a[i], x[i]), 1, 0, n - 1);
    for (auto &z : qry[i]) {
      pair<int, int> yo = get(z + 1, i, 1, 0, n - 1);
      go[z] = min(go[z], {yo.first, yo.second, x[z]});
    }
  }
  for (int i = n - 2; i >= 0; i--) {
    go[i] = min(go[i], go[i + 1]);
  }
  int cr = 0;
  vector<int> ans;
  while (1) {
    if (cr >= n || go[cr][0] == 2e9) break;
    int first = go[cr][1], second = go[cr][2];
    ans.push_back(kek[second]);
    ans.push_back(kek[first]);
    ans.push_back(kek[second]);
    ans.push_back(kek[first]);
    cr = go[cr][0] + 1;
  }
  cout << (int)ans.size() << endl;
  for (auto &z : ans) cout << z << ' ';
  return 0;
}
