#include <bits/stdc++.h>
using namespace std;
vector<int> ans;
int num[500005];
int n;
int get_ans(int start) {
  stack<int> st;
  map<int, int> between;
  map<int, int> times;
  for (int i = start; i < n; ++i) {
    if (between.find(num[i]) != between.end()) {
      ans.push_back(between[num[i]]);
      ans.push_back(num[i]);
      ans.push_back(between[num[i]]);
      ans.push_back(num[i]);
      return i;
    }
    while (!st.empty() &&
           (times[num[i]] > 1 || (times[num[i]] == 1 && st.top() != num[i]))) {
      int t = st.top();
      st.pop();
      between[t] = num[i];
      times[t]--;
    }
    st.push(num[i]);
    times[num[i]]++;
  }
  return -2;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> num[i];
  int st = 0;
  while (st != -1) {
    st = get_ans(st) + 1;
  }
  cout << ans.size() << endl;
  int sz = ans.size();
  for (int i = 0; i < sz; ++i) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}
