#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class _T>
inline _T loww(_T n) {
  return n & (-n);
}
template <class _T>
inline void swapp(_T &x, _T &y) {
  _T t;
  t = x, x = y, y = t;
}
template <class _T>
inline _T minn(_T i, _T j) {
  return i > j ? j : i;
}
template <class _T>
inline _T maxx(_T i, _T j) {
  return i > j ? i : j;
}
template <class _T>
inline _T gcd(_T b, _T c) {
  return b && c ? (c ? gcd(c, b % c) : b) : b | c;
}
template <class _T>
inline _T abss(_T b) {
  return b > 0 ? b : -b;
}
struct node {
  int v, col;
};
node a[500005 << 2];
void DOWN(int l, int r, int dq) {
  if (a[dq].col) {
    if (l != r) {
      a[dq << 1].col = a[dq << 1].v = a[dq << 1 | 1].col = a[dq << 1 | 1].v =
          a[dq].col;
    }
    a[dq].col = 0;
  }
}
void UP(int l, int r, int dq) {
  DOWN(l, r, dq);
  if (a[dq << 1].v == a[dq << 1 | 1].v)
    a[dq].v = a[dq << 1].v;
  else
    a[dq].v = -1;
}
void U(int l, int r, int dq, int b, int c, int v) {
  DOWN(l, r, dq);
  if (l == r) {
    a[dq].v = v;
  } else {
    if (b <= l && r <= c) {
      a[dq].v = a[dq].col = v;
    } else {
      int mid = (l + r) >> 1;
      if (b <= mid) U(l, mid, dq << 1, b, c, v);
      if (c > mid) U(mid + 1, r, dq << 1 | 1, b, c, v);
      UP(l, r, dq);
    }
  }
}
int Q(int l, int r, int dq, int b) {
  DOWN(l, r, dq);
  if (l == r || a[dq].v != -1)
    return a[dq].v;
  else {
    int mid = (l + r) >> 1;
    if (b <= mid)
      return Q(l, mid, dq << 1, b);
    else
      return Q(mid + 1, r, dq << 1 | 1, b);
  }
}
void build(int l, int r, int dq) {
  if (l == r) {
    a[dq].col = a[dq].v = 0;
  } else {
    a[dq].col = a[dq].v = 0;
    int mid = (l + r) >> 1;
    build(l, mid, dq << 1);
    build(mid + 1, r, dq << 1 | 1);
  }
}
bool outflag = true;
void out(int b) {
  if (outflag)
    outflag = false;
  else
    printf(" ");
  printf("%d", b);
}
pair<int, int> goal[500005];
int val[500005];
map<int, int> mymap;
int pre[500005], gl = 0;
int en = 0;
int main() {
  int n, i, t, js, told, b;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &val[i]);
  }
  build(1, n, 1);
  for (i = 1; i <= n; i++) {
    pre[i] = mymap[val[i]];
    mymap[val[i]] = i;
    t = pre[i];
    told = i;
    js = 0;
    while (t > en) {
      if (js++ > 1) break;
      if (t + 1 <= told - 1) U(1, n, 1, t + 1, told - 1, val[i]);
      b = Q(1, n, 1, t);
      if (b != 0) {
        goal[gl++] = make_pair(b, val[i]);
        en = i;
        break;
      }
      told = t;
      t = pre[t];
    }
    if (js == 3) {
      goal[gl++] = make_pair(val[i], val[i]);
      en = i;
    }
  }
  printf("%d\n", gl * 4);
  for (i = 0; i < gl; i++) {
    out(goal[i].first);
    out(goal[i].second);
    out(goal[i].first);
    out(goal[i].second);
  }
  printf("\n");
  return 0;
}
