#include <bits/stdc++.h>
using namespace std;
int n, ans[500005], len, t[500005], fr[500005];
int a[500005], prv[500005], Fr[500005], Val[500005];
struct el {
  int poz, val;
  bool operator<(const el &A) const { return val < A.val; }
} v[500005];
inline int Next(int p) {
  int rad, cp = p;
  while (t[p] != p) p = t[p];
  rad = p;
  while (t[cp] != cp) {
    p = t[cp];
    t[cp] = rad;
    cp = p;
  }
  return rad;
}
inline void Mark(int x, int y, int val) {
  if (x > y) return;
  while (y >= x) {
    if (Next(y) == y) {
      Fr[a[y]] = val;
      t[y] = x - 1;
      --y;
    } else
      y = Next(y);
  }
}
inline void Solve(int poz) {
  int i, j;
  for (i = poz; i <= n; ++i)
    if (Fr[a[i]]) {
      ans[++len] = Fr[a[i]];
      ans[++len] = Val[a[i]];
      ans[++len] = Fr[a[i]];
      ans[++len] = Val[a[i]];
      for (j = poz; j <= i; ++j) Fr[a[j]] = 0;
      Solve(i + 1);
      return;
    } else if (prv[i] && prv[i] >= poz) {
      if (prv[prv[i]] && prv[prv[i]] >= poz)
        Mark(prv[i], i - 1, Val[a[i]]);
      else
        Mark(prv[i] + 1, i - 1, Val[a[i]]);
    }
}
inline void Normalize() {
  sort(v + 1, v + n + 1);
  a[v[1].poz] = 1;
  Val[1] = v[1].val;
  for (int i = 2; i <= n; ++i) {
    a[v[i].poz] = a[v[i - 1].poz];
    if (v[i].val > v[i - 1].val) ++a[v[i].poz];
    Val[a[v[i].poz]] = v[i].val;
  }
}
inline void Make_Prv() {
  int i;
  for (i = 1; i <= n; ++i) {
    prv[i] = fr[a[i]];
    fr[a[i]] = i;
  }
}
int main() {
  int i, j;
  cin >> n;
  for (i = 1; i <= n; ++i) {
    cin >> a[i];
    v[i].val = a[i];
    v[i].poz = i;
  }
  Normalize();
  Make_Prv();
  for (i = 1; i <= n; ++i) t[i] = i;
  Solve(1);
  cout << len << "\n";
  for (i = 1; i <= len; ++i) cout << ans[i] << " ";
  cout << "\n";
  return 0;
}
