#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 2;
pair<int, int> seg[4 * maxn];
int a[maxn];
map<int, vector<int>> V;
void add(int id, int L, int R, int idx, int val) {
  if (idx < L || idx >= R) return;
  if (R == L + 1) {
    seg[id] = pair<int, int>(val, idx);
    return;
  }
  int mid = (L + R) / 2;
  add(2 * id + 0, L, mid, idx, val);
  add(2 * id + 1, mid, R, idx, val);
  seg[id] = max(seg[2 * id], seg[2 * id + 1]);
}
pair<int, int> get(int id, int L, int R, int l, int r) {
  if (r <= L || R <= l) return pair<int, int>(-1, -1);
  if (L >= l && R <= r) return seg[id];
  int mid = (L + R) / 2;
  return max(get(2 * id, L, mid, l, r), get(2 * id + 1, mid, R, l, r));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  fill(seg, seg + 4 * n, pair<int, int>(-1, 0));
  vector<int> vec;
  map<int, int> M;
  for (int i = 0; i < n; i++) {
    int x;
    if (vec.size())
      x = vec.back();
    else
      x = -1;
    if (M.find(a[i]) != M.end() && M[a[i]] > x) {
      int idx = M[a[i]];
      pair<int, int> P = get(1, 0, n, 0, idx);
      if (P.first > idx) {
        for (int j = x + 1; j <= i; j++) {
          add(1, 0, n, j, -1);
        }
        vec.push_back(P.second);
        vec.push_back(idx);
        vec.push_back(P.first);
        vec.push_back(i);
      } else {
        if (V[a[i]].size() > 2 && V[a[i]][V[a[i]].size() - 3] > x) {
          for (int j = x + 1; j <= i; j++) add(1, 0, n, j, -1);
          vec.push_back(V[a[i]][V[a[i]].size() - 3]);
          vec.push_back(V[a[i]][V[a[i]].size() - 2]);
          vec.push_back(V[a[i]][V[a[i]].size() - 1]);
          vec.push_back(i);
        } else
          add(1, 0, n, idx, i);
      }
    }
    M[a[i]] = i;
    V[a[i]].push_back(i);
  }
  cout << vec.size() << '\n';
  for (auto b : vec) cout << a[b] << ' ';
}
