#include <bits/stdc++.h>
using namespace std;
constexpr long long mod = 1000000007;
const long long INF = mod * mod;
const long double eps = 1e-12;
const long double pi = acos(-1.0);
struct SegT {
 private:
  int sz;
  vector<int> node;
  const int init_c = mod;

 public:
  SegT(int n) {
    sz = 1;
    while (sz < n) sz *= 2;
    node.resize(2 * sz - 1, init_c);
  }
  int f(int a, int b) { return min(a, b); }
  void update(int k, int a) {
    k += sz - 1;
    node[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = sz;
    if (r <= a || b <= l)
      return init_c;
    else if (a <= l && r <= b)
      return node[k];
    else {
      int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return f(vl, vr);
    }
  }
};
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<int> x;
  for (int i = 0; i < n; i++) {
    x.push_back(a[i]);
  }
  sort((x).begin(), (x).end());
  x.erase(unique((x).begin(), (x).end()), x.end());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound((x).begin(), (x).end(), a[i]) - x.begin();
  }
  vector<vector<int>> locs(x.size());
  for (int i = 0; i < n; i++) {
    locs[a[i]].push_back(i);
  }
  vector<int> ri(n, mod);
  vector<int> pre(n, -1);
  for (int i = 0; i < x.size(); i++) {
    for (int j = 0; j < (int)locs[i].size() - 1; j++) {
      ri[locs[i][j]] = locs[i][j + 1];
      pre[locs[i][j + 1]] = locs[i][j];
    }
  }
  vector<int> r(n, mod);
  SegT st(n);
  for (int i = 0; i < n; i++) {
    if (pre[i] < 0) {
      st.update(i, ri[i]);
      continue;
    }
    r[pre[i]] = st.query(pre[i] + 1, i);
    st.update(pre[i], mod);
    st.update(i, ri[i]);
  }
  for (int i = 0; i < x.size(); i++) {
    for (int j = 0; j < (int)locs[i].size() - 3; j++) {
      int f = locs[i][j];
      int t = locs[i][j + 3];
      r[f] = min(r[f], t);
    }
  }
  vector<int> tos(n + 1);
  vector<pair<int, int>> ps(n + 1);
  tos[n] = mod;
  ps[n] = {mod, 0};
  for (int i = n - 1; i >= 0; i--) {
    if (r[i] < tos[i + 1]) {
      tos[i] = r[i];
      ps[i] = {i, r[i]};
    } else {
      tos[i] = tos[i + 1];
      ps[i] = ps[i + 1];
    }
  }
  vector<int> ans;
  int cur = 0;
  while (cur < n) {
    if (tos[cur] < mod) {
      int s1 = x[a[ps[cur].first]];
      int s2 = x[a[ps[cur].second]];
      for (int j = 0; j < 2; j++) {
        ans.push_back(s1);
        ans.push_back(s2);
      }
      cur = tos[cur] + 1;
    } else
      break;
  }
  cout << ans.size() << "\n";
  for (int i = 0; i < ans.size(); i++) {
    if (i > 0) cout << " ";
    cout << ans[i];
  }
  cout << "\n";
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  char nyaa;
  cin >> nyaa;
  return 0;
}
