#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
vector<int> T;
int l, size;
void Stworz(int n) {
  int p = 1;
  while (p < n) p *= 2;
  l = p;
  size = 2 * p - 1;
  for (int i = 0; i <= size; ++i) T.push_back(INF);
}
void Ustaw(int a, int x) {
  a += l;
  T[a] = x;
  a /= 2;
  while (a != 0) {
    T[a] = min(T[2 * a], T[2 * a + 1]);
    a /= 2;
  }
}
int Mini(int a, int b) {
  a += l;
  b += l;
  int m = min(T[a], T[b]);
  while (a / 2 != b / 2) {
    if (a % 2 == 0) m = min(m, T[a + 1]);
    if (b % 2 == 1) m = min(m, T[b - 1]);
    a /= 2;
    b /= 2;
  }
  return m;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> wyst, pier;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    wyst[a[i]] = 0;
    pier[a[i]] = INF;
  }
  Stworz(n);
  vector<int> odp;
  for (int L = 0; L < n;) {
    int P = L;
    while (P < n) {
      ++wyst[a[P]];
      if (pier[a[P]] == INF) pier[a[P]] = P;
      Ustaw(P, pier[a[P]]);
      if (pier[a[P]] + 1 <= P - 1) {
        int ind = Mini(pier[a[P]] + 1, P - 1);
        if (ind < pier[a[P]]) {
          odp.push_back(a[ind]);
          odp.push_back(a[P]);
          odp.push_back(a[ind]);
          odp.push_back(a[P]);
          break;
        }
      }
      if (wyst[a[P]] >= 4) {
        for (int i = 0; i < 4; ++i) odp.push_back(a[P]);
        break;
      }
      ++P;
    }
    for (int i = L; i <= min(P, n); ++i) {
      --wyst[a[i]];
      pier[a[i]] = INF;
    }
    L = P + 1;
  }
  cout << odp.size() << endl;
  for (int i = 0; i < odp.size(); ++i) cout << odp[i] << " ";
  return 0;
}
