#include <bits/stdc++.h>
using namespace std;
using namespace std::rel_ops;
struct TN {
  int k, v, p, m;
  TN *l, *r;
  TN(int K, int V) : k(K), v(V), p(rand()), m(V), l(0), r(0) {}
};
class Treap {
  TN *root;
  int getMax(TN *n) { return n ? n->m : -1; }
  void update(TN *n) {
    if (n) n->m = max(n->v, max(getMax(n->l), getMax(n->r)));
  }
  TN *merge(TN *a, TN *b) {
    if (!a || !b) return a ? a : b;
    if (a->p > b->p) {
      a->r = merge(a->r, b);
      update(a);
      return a;
    } else {
      b->l = merge(a, b->l);
      update(b);
      return b;
    }
  }
  void split(TN *t, int k, TN *&a, TN *&b) {
    if (!t) {
      a = b = 0;
      return;
    }
    if (t->k <= k) {
      split(t->r, k, t->r, b);
      a = t;
    } else {
      split(t->l, k, a, t->l);
      b = t;
    }
    update(a);
    update(b);
  }
  void destroy(TN *&n) {
    if (!n) return;
    destroy(n->l);
    destroy(n->r);
    delete n;
    n = 0;
  }

 public:
  Treap() : root(0) {}
  void insert(int k, int v) {
    TN *a, *b;
    split(root, k - 1, a, b);
    root = merge(merge(a, new TN(k, v)), b);
  }
  void update(int k, int v) {
    TN *a, *b, *c;
    split(root, k - 1, a, b);
    split(root, k, b, c);
    b->v = v;
    update(b);
    root = merge(merge(a, b), c);
  }
  int maxEndOfBeginningBefore(int k) {
    TN *a, *b;
    split(root, k - 1, a, b);
    int res = getMax(a);
    root = merge(a, b);
    return res;
  }
  void clear() { destroy(root); }
} t;
map<int, int> firstPos, cnt;
set<int> s;
int n, a[500010];
vector<int> res;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    cnt[a[i]]++;
    if (cnt[a[i]] == 4) {
      res.push_back(a[i]);
      res.push_back(a[i]);
      res.push_back(a[i]);
      res.push_back(a[i]);
      t.clear();
      firstPos.clear();
      s.clear();
      cnt.clear();
      continue;
    }
    if (firstPos.find(a[i]) == firstPos.end()) {
      firstPos[a[i]] = i;
      continue;
    }
    int fp = firstPos[a[i]];
    int maxLast = t.maxEndOfBeginningBefore(fp);
    if (maxLast > fp) {
      res.push_back(a[maxLast]);
      res.push_back(a[i]);
      res.push_back(a[maxLast]);
      res.push_back(a[i]);
      t.clear();
      firstPos.clear();
      s.clear();
      cnt.clear();
      continue;
    }
    t.insert(fp, i);
  }
  printf("%d\n", res.size());
  for (int i = 0; i < res.size(); i++) printf("%d ", res[i]);
}
