#include <bits/stdc++.h>
using namespace std;
int segs[2000050];
int find(int idx, int l, int r, int pos) {
  if (r - l == 1) return segs[idx];
  if (segs[idx] != -1) return segs[idx];
  int mid = (l + r) / 2;
  if (pos < mid) {
    return find(2 * idx, l, mid, pos);
  } else
    return find(2 * idx + 1, mid, r, pos);
}
void insert(int idx, int l, int r, int lpos, int rpos, int val) {
  if (segs[idx] != -1) return;
  if (l == lpos && r == rpos) {
    segs[idx] = val;
    return;
  }
  int mid = (l + r) / 2;
  if (mid >= rpos) {
    insert(2 * idx, l, mid, lpos, rpos, val);
  } else if (mid <= lpos) {
    insert(2 * idx + 1, mid, r, lpos, rpos, val);
  } else {
    insert(2 * idx, l, mid, lpos, mid, val);
    insert(2 * idx + 1, mid, r, mid, rpos, val);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < 2000050; i++) segs[i] = -1;
  vector<int> ans;
  unordered_map<int, vector<int> > nums;
  for (int i = 0; i < n; i++) {
    bool found = false;
    int next;
    scanf("%d", &next);
    auto x = nums.find(next);
    if (x == nums.end()) {
      nums.insert({next, vector<int>(1, i)});
    } else if (x->second.size() == 3) {
      for (int j = 0; j < 4; j++) ans.push_back(next);
      found = true;
    } else {
      for (int idx : x->second) {
        int val = find(1, 0, n, idx);
        if (val != -1) {
          found = true;
          ans.push_back(val);
          ans.push_back(next);
          ans.push_back(val);
          ans.push_back(next);
          break;
        }
      }
    }
    if (x != nums.end()) {
      if (x->second[0] + 1 < i) insert(1, 0, n, x->second[0] + 1, i, next);
      x->second.push_back(i);
    }
    if (found) {
      nums = unordered_map<int, vector<int> >(0);
    }
  }
  printf("%d\n", (int)ans.size());
  for (int x : ans) printf("%d ", x);
}
