#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;
const bool db = false;
vector<pair<int, int> > seg;
vector<int> ans;
unordered_map<int, int> last;
int n, a[500010], Border;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    if (last[a[i]] > Border) {
      seg.push_back(make_pair(last[a[i]], i));
      for (int j = seg.size() - 2, it = 0; j >= 0 && it < 1500; ++it, --j)
        if (seg[j].first < seg.back().first &&
            seg.back().first < seg[j].second) {
          ans.push_back(seg[j].first);
          ans.push_back(seg.back().first);
          ans.push_back(seg[j].second);
          ans.push_back(seg.back().second);
          Border = i;
          seg.clear();
          break;
        } else if (a[seg[j].first] == a[i] &&
                   seg[j].second != seg.back().first) {
          ans.push_back(seg[j].first);
          ans.push_back(seg[j].second);
          ans.push_back(seg.back().first);
          ans.push_back(seg.back().second);
          Border = i;
          seg.clear();
          break;
        }
    }
    last[a[i]] = i;
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); ++i) printf("%d ", a[ans[i]]);
  return 0;
}
