#include <bits/stdc++.h>
using namespace std;
const long long ll_INF = ((unsigned long long)(-1)) >> 1;
const double EPS = 1e-8;
const int INF = 100000000;
const int MAXN = 500000 + 100;
struct Node {
  int l, r;
  int x;
} nodes[MAXN];
map<int, int> pos, cnt;
vector<int> b;
int num[MAXN], n, top;
void read() {
  b.clear();
  top = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
}
void push(int x, int y) {
  b.push_back(x);
  b.push_back(y);
  b.push_back(x);
  b.push_back(y);
  cnt.clear();
  pos.clear();
  top = 0;
}
int main() {
  while (~scanf("%d", &n)) {
    read();
    for (int i = 1; i <= n; i++) {
      int x = num[i];
      if (cnt[x] != 0)
        cnt[x]++;
      else
        cnt[x] = 1;
      if (cnt[x] == 4) {
        push(x, x);
        continue;
      }
      if (pos[x] == 0) {
        pos[x] = i;
        continue;
      }
      int l = pos[x], r = i;
      pos[x] = i;
      while (top > 0) {
        int bl = nodes[top - 1].l, br = nodes[top - 1].r, bx = nodes[top - 1].x;
        if (l > bl && l < br) {
          push(bx, x);
          break;
        } else if (l <= bl)
          top--;
        else {
          nodes[top].l = l;
          nodes[top].r = r;
          nodes[top].x = x;
          top++;
          break;
        }
      }
      if (top == 0) {
        nodes[0].l = l;
        nodes[0].r = r;
        nodes[0].x = x;
        top++;
      }
    }
    printf("%d\n", b.size());
    if (b.size()) printf("%d", b[0]);
    for (int i = 1; i < b.size(); i++) printf(" %d", b[i]);
    putchar('\n');
  }
  return 0;
}
