#include <bits/stdc++.h>
using namespace std;
const int maxN = 600000;
int* t[2 * maxN];
int n, a[maxN], b[maxN];
unordered_map<int, int> pos;
int d[maxN][2];
int p[maxN];
char s[maxN * 10];
int res[maxN];
void build(int i, int l, int r) {
  if (l == r) {
    t[i] = new int[1];
    t[i][0] = b[l];
  } else {
    build(2 * i, l, (l + r) / 2);
    build(2 * i + 1, (l + r) / 2 + 1, r);
    t[i] = new int[r - l + 1];
    merge(t[2 * i], t[2 * i] + (l + r) / 2 - l + 1, t[2 * i + 1],
          t[2 * i + 1] + r - (l + r) / 2, t[i]);
  }
}
int get(int i, int cl, int cr, int l, int r, int value) {
  if (l > r) {
    return n;
  }
  if (cl == l && r == cr) {
    int left_bound = 0, right_bound = r - l;
    while (right_bound - left_bound > 1) {
      int middle = (left_bound + right_bound) / 2;
      if (t[i][middle] > value) {
        right_bound = middle;
      } else {
        left_bound = middle;
      }
    }
    if (t[i][left_bound] > value) {
      return t[i][left_bound];
    }
    if (t[i][right_bound] > value) {
      return t[i][right_bound];
    }
    return n;
  }
  if (l > (cl + cr) / 2) {
    return get(2 * i + 1, (cl + cr) / 2 + 1, cr, l, r, value);
  }
  if (r <= (cl + cr) / 2) {
    return get(2 * i, cl, (cl + cr) / 2, l, r, value);
  }
  int A = get(2 * i, cl, (cl + cr) / 2, l, (cl + cr) / 2, value);
  int B = get(2 * i + 1, (cl + cr) / 2 + 1, cr, (cl + cr) / 2 + 1, r, value);
  return min(A, B);
}
int main() {
  memset(b, -1, sizeof(b));
  scanf("%d\n", &n);
  gets(s);
  int m = strlen(s);
  int current = 0, k = 0;
  for (int i = 0; i <= m; ++i) {
    if (i == m || s[i] == ' ') {
      a[k++] = current;
      current = 0;
    } else {
      current = current * 10 + (s[i] - '0');
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    if (pos.count(a[i])) {
      b[i] = pos[a[i]];
    }
    pos[a[i]] = i;
  }
  build(1, 0, n - 1);
  p[n] = n;
  d[n][0] = d[n][1] = -1;
  for (int i = n - 1; i >= 0; --i) {
    int j = b[i];
    if (j == -1 || j + 1 >= p[i + 1]) {
      p[i] = p[i + 1];
      d[i][0] = d[i + 1][0];
      d[i][1] = d[i + 1][1];
      continue;
    }
    int value = get(1, 0, n - 1, i + 1, j - 1, j);
    if (value == n) {
      p[i] = p[i + 1];
      d[i][0] = d[i + 1][0];
      d[i][1] = d[i + 1][1];
    } else {
      p[i] = value;
      d[i][0] = a[i];
      d[i][1] = a[value];
    }
    if (p[i + 1] < p[i]) {
      p[i] = p[i + 1];
      d[i][0] = d[i + 1][0];
      d[i][1] = d[i + 1][1];
    }
    int k = b[j];
    if (k != -1) {
      int l = b[k];
      if (l != -1 && l < p[i]) {
        p[i] = l;
        d[i][0] = d[i][1] = a[i];
      }
    }
  }
  int g = 0;
  int start = 0;
  while (start < n) {
    if (p[start] != n) {
      res[g++] = d[start][0];
      res[g++] = d[start][1];
      res[g++] = d[start][0];
      res[g++] = d[start][1];
      start = p[start] + 1;
    } else {
      break;
    }
  }
  printf("%d\n", g);
  memset(s, 0, sizeof(s));
  int w = 0;
  for (int i = 0; i < g; ++i) {
    char p[10];
    int r = 0;
    while (res[i]) {
      p[r++] = (res[i] % 10) + '0';
      res[i] /= 10;
    }
    if (r == 0) {
      p[r++] = '0';
    }
    for (int j = r - 1; j >= 0; --j) {
      s[w++] = p[j];
    }
    s[w++] = ' ';
  }
  puts(s);
  return 0;
}
