#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
struct node {
  int l, r;
  int num;
};
node v[N];
int b[N];
int id;
map<int, int> cont, pos;
vector<int> ans;
void get_ans(int a, int b) {
  ans.push_back(a);
  ans.push_back(b);
  ans.push_back(a);
  ans.push_back(b);
  cont.clear();
  pos.clear();
  id = 0;
}
void solve() {
  int n;
  id = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  for (int i = 1; i <= n; i++) {
    int u = b[i];
    cont[u]++;
    if (cont[u] == 4) {
      get_ans(u, u);
      continue;
    }
    if (pos[u] == 0) {
      pos[u] = i;
      continue;
    }
    int pre = pos[u];
    int now = i;
    pos[u] = now;
    while (id > 0) {
      int index = id - 1;
      int now_l = v[index].l;
      int now_r = v[index].r;
      int now_num = v[index].num;
      if (now_l < pre && now_r > pre) {
        get_ans(now_num, u);
        break;
      } else if (pre <= now_l)
        id--;
      else {
        v[id].l = pre;
        v[id].r = now;
        v[id].num = u;
        id++;
        break;
      }
    }
    if (id == 0) {
      v[id].l = pre;
      v[id].r = now;
      v[id].num = u;
      id++;
    }
  }
  cout << ans.size() << endl;
  for (int i = 0; i < (int)ans.size(); i++) {
    cout << ans[i] << ' ';
  }
  cout << endl;
}
int main() {
  solve();
  return 0;
}
