#include <bits/stdc++.h>
using namespace std;
int n, x;
vector<int> ans, st;
map<int, int> cnt, cov;
int main() {
  cin >> n;
  while (n--) {
    cin >> x;
    if (cnt[x] == 3) cov[x] = x;
    if (cov[x]) {
      ans.push_back(cov[x]), ans.push_back(x), ans.push_back(cov[x]),
          ans.push_back(x);
      st.clear(), cnt.clear(), cov.clear();
      continue;
    }
    while (st.size() && cnt[x] >= 1 && st.back() != x)
      cov[st.back()] = x, cnt[st.back()]--, st.pop_back();
    cnt[x]++, st.push_back(x);
  }
  cout << ans.size() << '\n';
  for (auto i : ans) cout << i << ' ';
}
