#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000000")
using namespace std;
int n, arr[500001];
map<int, int> mp;
vector<int> ans, adj[500001];
set<int> pos;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  int id = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (mp.find(arr[i]) == mp.end()) mp[arr[i]] = id++;
    adj[mp[arr[i]]].push_back(i);
  }
  int lim = n + 1, b1, b2;
  for (int i = 0; i < n; i++) {
    if (i == lim) {
      ans.push_back(b1);
      ans.push_back(b2);
      ans.push_back(b1);
      ans.push_back(b2);
      lim = n + 1;
      pos.clear();
      continue;
    }
    while (pos.size() && *pos.begin() <= i) {
      int x = *pos.begin();
      pos.erase(pos.begin());
      int id = mp[arr[x]];
      int ind =
          upper_bound((adj[id]).begin(), (adj[id]).end(), i) - adj[id].begin();
      if (ind < adj[id].size()) pos.insert(adj[id][ind]);
    }
    int id = mp[arr[i]];
    if (pos.size()) {
      int p2 = upper_bound((adj[id]).begin(), (adj[id]).end(), *pos.begin()) -
               adj[id].begin();
      if (p2 < adj[id].size()) {
        p2 = adj[id][p2];
        if (lim > p2) {
          lim = p2;
          b1 = arr[*pos.begin()];
          b2 = arr[i];
        }
      }
    }
    int p2 =
        upper_bound((adj[id]).begin(), (adj[id]).end(), i) - adj[id].begin();
    if (p2 < adj[id].size()) pos.insert(adj[id][p2]);
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
  return 0;
}
