#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "[";
  for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
    if (it != t.begin()) os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &t) {
  os << "{";
  for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
    if (it != t.begin()) os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
const int INF = 1 << 28;
const double EPS = 1e-8;
const int MOD = 1000000007;
static const long long int defvalue = 0;
class SegTree {
 private:
  vector<pair<int, int> > val;
  int n;
  pair<int, int> combine(pair<int, int> a, pair<int, int> b) {
    return max(a, b);
  }

 public:
  SegTree(int size) {
    n = 1;
    while (n < size) n <<= 1;
    val = vector<pair<int, int> >(2 * n, pair<int, int>(0, 0));
  }
  pair<int, int> get(int i) {
    pair<int, int> ret(0, 0);
    i += n - 1;
    ret = val[i];
    while (i > 0) {
      i = (i - 1) / 2;
      ret = max(ret, val[i]);
    }
    return ret;
  }
  void set(int a, int b, pair<int, int> x, int k = 0, int l = 0, int r = -1) {
    if (r == -1) r = n;
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) {
      val[k] = x;
    } else {
      set(a, b, x, k * 2 + 1, l, (l + r) / 2);
      set(a, b, x, k * 2 + 2, (l + r) / 2, r);
    }
  }
};
int n;
int main() {
  map<int, int> prev;
  map<int, int> cnt;
  scanf("%d", &n);
  SegTree seg(n + 1);
  vector<int> ans;
  int end = 1;
  int first = INF, last = INF;
  int firstc;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    cnt[x]++;
    if (cnt[x] > 3) {
      for (int j = 0; j < (int)(4); j++) ans.push_back(x);
      prev.clear();
      cnt.clear();
      first = INF;
      last = INF;
    } else {
      if (prev[x] < end)
        prev[x] = i;
      else {
        pair<int, int> t = seg.get(prev[x]);
        if (!t.first) {
          seg.set(prev[x] + 1, i, pair<int, int>(i, x));
          prev[x] = i;
          firstc = x;
          last = i;
        } else {
          for (int j = 0; j < (int)(2); j++) {
            ans.push_back(t.second);
            ans.push_back(x);
          }
          prev.clear();
          cnt.clear();
          first = INF;
          last = INF;
        }
      }
    }
  }
  printf("%d\n", ans.size());
  for (__typeof((ans).begin()) it = (ans).begin(); it != (ans).end(); it++)
    printf("%d ", *it);
  return 0;
}
