#include <bits/stdc++.h>
using namespace std;
const int maxN = 5e5 + 13;
int n, a[maxN], check, ans, pre[maxN], mp[maxN], pre2[maxN];
map<int, int> lst;
vector<int> vec, seq, val;
map<int, int> mark;
struct seg_tree {
  vector<int> tree[maxN * 4];
  void mrg(const vector<int> &a, const vector<int> &b, vector<int> &c) {
    int indx1 = 0, indx2 = 0;
    while (indx1 < a.size() && indx2 < b.size()) {
      if (a[indx1] < b[indx2])
        c.push_back(a[indx1++]);
      else
        c.push_back(b[indx2++]);
    }
    while (indx1 < a.size()) c.push_back(a[indx1++]);
    while (indx2 < b.size()) c.push_back(b[indx2++]);
  }
  void build(int t, int l = 0, int r = n, int indx = 1) {
    if (r - l <= 1) {
      if (!t)
        tree[indx].push_back(pre[l]);
      else
        tree[indx].push_back(pre2[l]);
      return;
    }
    int mid = l + r >> 1;
    build(t, l, mid, indx << 1);
    build(t, mid, r, indx << 1 ^ 1);
    mrg(tree[indx << 1], tree[indx << 1 ^ 1], tree[indx]);
  }
  int get(int L, int R, int val, int l = 0, int r = n, int indx = 1) {
    if (L <= l && r <= R) {
      auto it = lower_bound(tree[indx].begin(), tree[indx].end(), val);
      if (it == tree[indx].begin()) return -1;
      it--;
      return (*it);
    }
    if (R <= l || r <= L) return -1;
    int mid = l + r >> 1;
    return max(get(L, R, val, l, mid, indx << 1),
               get(L, R, val, mid, r, indx << 1 ^ 1));
  }
} seg[2];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (lst.find(a[i]) != lst.end())
      pre[i] = lst[a[i]];
    else
      pre[i] = i;
    if (pre[i] != i)
      pre2[i] = pre[pre[i]];
    else
      pre2[i] = i;
    mp[pre[i]] = a[i];
    lst[a[i]] = i;
  }
  seg[0].build(0);
  seg[1].build(1);
  int lst = -1;
  for (int i = 0; i < n; i++) {
    if (pre[i] != i) {
      int tmp = seg[0].get(pre[i] + 1, i, pre[i]);
      int tmp2 = seg[1].get(pre2[i] + 1, i, pre2[i]);
      if (tmp != -1 && lst < tmp) {
        seq.push_back(mp[tmp]);
        seq.push_back(a[i]);
        seq.push_back(mp[tmp]);
        seq.push_back(a[i]);
        lst = i;
        ans++;
      } else if (tmp2 != -1 && lst < tmp2) {
        seq.push_back(mp[tmp2]);
        seq.push_back(a[i]);
        seq.push_back(mp[tmp2]);
        seq.push_back(a[i]);
        lst = i;
        ans++;
      }
    }
  }
  cout << ans * 4 << endl;
  for (auto a : seq) cout << a << " ";
}
