#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
vector<int> ans;
int n;
void add(int x, int y) {
  ans.push_back(x);
  ans.push_back(y);
  ans.push_back(x);
  ans.push_back(y);
}
void solve(int &i) {
  map<int, int> between, left, cnt;
  stack<int> st;
  for (int x; i <= n; ++i) {
    scanf("%d", &x);
    if (between[x]) {
      add(between[x], x);
      return;
    }
    if (++cnt[x] == 4) {
      add(x, x);
      return;
    }
    if (left[x]) {
      while (st.size()) {
        int y = st.top();
        st.pop();
        if (y == x) break;
        between[y] = x;
      }
    }
    left[x] = i;
    st.push(x);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) solve(i);
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i]);
  return 0;
}
