#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int n;
vector<int> v, lm, rm;
class SegmentTreeMax {
 public:
  int n;
  vector<long long> vect;
  SegmentTreeMax(int _n) : n(_n) {
    vect.resize(2 * n);
    fill(vect.begin(), vect.end(), 0);
  }
  void modify(int p, long long value) {
    for (vect[p += n] = value; p > 1; p >>= 1) {
      int val = rm[vect[p]] > rm[vect[p ^ 1]] ? vect[p] : vect[p ^ 1];
      vect[p >> 1] = val;
    }
  }
  long long query(int l, int r) {
    long long res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) {
        if (rm[res] < rm[vect[l]]) res = vect[l];
        l++;
      }
      if (r & 1) {
        if (rm[res] < rm[vect[r - 1]]) res = vect[r - 1];
        r--;
      }
    }
    return res;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  v.resize(n + 1);
  lm.resize(n + 1);
  rm.resize(n + 1);
  vector<int> v2;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    v2.push_back(v[i]);
  }
  v2.push_back(0);
  sort(v2.begin(), v2.end());
  auto it = unique(v2.begin(), v2.end());
  v2.resize(it - v2.begin());
  int l = 1, r = 1;
  vector<int> ans;
  SegmentTreeMax stm(n + 1);
  vector<int> visited, ct(n + 1);
  vector<vector<int>> updates(n + 1);
  while (r <= n) {
    int id = lower_bound(v2.begin(), v2.end(), v[r]) - v2.begin();
    if (!lm[id]) lm[id] = r;
    int val = stm.query(l, lm[id]);
    updates[id].push_back(r);
    rm[id] = r;
    ct[id]++;
    visited.push_back(id);
    if (rm[val] > lm[id] || ct[id] == 4) {
      if (ct[id] == 4) {
        for (int i = 0; i < 4; i++) ans.push_back(v[r]);
      } else {
        ans.push_back(v2[val]);
        ans.push_back(v[r]);
        ans.push_back(v2[val]);
        ans.push_back(v[r]);
      }
      for (int i : visited) {
        lm[i] = 0;
        rm[i] = 0;
        ct[i] = 0;
        updates[i].clear();
      }
      visited.clear();
      l = r + 1;
      r = l;
    } else {
      for (int i : updates[id]) {
        stm.modify(i, id);
      }
      r++;
    }
  }
  cout << ans.size() << '\n';
  for (int i : ans) {
    cout << i << ' ';
  }
  return 0;
}
