#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e18;
const long long MOD = (long long)1e9 + 7;
int n, rd[600000], bs[600000], cnt;
map<int, int> s;
int t[3000000], tM[3000000];
vector<int> v[600000];
void updT(int v, int l, int r, int ind, int val) {
  if (l == r) {
    t[v] = val;
    return;
  }
  int m = (l + r) / 2;
  if (ind <= m)
    updT(v * 2, l, m, ind, val);
  else
    updT(v * 2 + 1, m + 1, r, ind, val);
  t[v] = ((t[v * 2]) > (t[v * 2 + 1]) ? (t[v * 2]) : (t[v * 2 + 1]));
}
int getT(int v, int l, int r, int L, int R) {
  if (L > R) return 0;
  if (l == L && r == R) return t[v];
  int m = (l + r) / 2;
  return (
      (getT(v * 2, l, m, L, ((m) < (R) ? (m) : (R)))) >
              (getT(v * 2 + 1, m + 1, r, ((L) > (m + 1) ? (L) : (m + 1)), R))
          ? (getT(v * 2, l, m, L, ((m) < (R) ? (m) : (R))))
          : (getT(v * 2 + 1, m + 1, r, ((L) > (m + 1) ? (L) : (m + 1)), R)));
}
vector<int> ans;
int step, curstep[600000];
int main() {
  int i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%d", &rd[i]);
    if (!s[rd[i]]) s[rd[i]] = ++cnt, bs[cnt] = rd[i];
    rd[i] = s[rd[i]];
  }
  for (i = 1; i <= n; ++i) {
    if (curstep[rd[i]] != step) v[rd[i]].clear();
    curstep[rd[i]] = step;
    char cont = 0;
    for (j = 0; j < (int)v[rd[i]].size(); ++j) {
      int gettmp = getT(1, 1, 500000, 1, v[rd[i]][j] - 1);
      if (gettmp > v[rd[i]][j] && gettmp < i) {
        ans.push_back(rd[gettmp]);
        ans.push_back(rd[i]);
        ans.push_back(rd[gettmp]);
        ans.push_back(rd[i]);
        cont = 1;
        ++step;
        break;
      }
    }
    if (cont) continue;
    for (j = 0; j < (int)v[rd[i]].size(); ++j)
      updT(1, 1, 500000, v[rd[i]][j], i);
    v[rd[i]].push_back(i);
    if ((int)v[rd[i]].size() == 4) {
      ans.push_back(rd[i]);
      ans.push_back(rd[i]);
      ans.push_back(rd[i]);
      ans.push_back(rd[i]);
      ++step;
      continue;
    }
  }
  printf("%d\n", (int)ans.size());
  for (i = 0; i < (int)ans.size(); ++i) printf("%d ", bs[ans[i]]);
}
