#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
const int INF = 1e9 + 239;
struct Query {
  int l;
  int r;
  int x;
  int *pnt_ans;
  Query() {}
  Query(int l_, int r_, int x_, int *pnt_ans_) {
    l = l_;
    r = r_;
    x = x_;
    pnt_ans = pnt_ans_;
  }
};
namespace SegmentTree {
int n;
int t[4 * N];
int ti[4 * N];
void pull(int v) {
  if (t[2 * v + 1] <= t[2 * v + 2]) {
    t[v] = t[2 * v + 1];
    ti[v] = ti[2 * v + 1];
  } else {
    t[v] = t[2 * v + 2];
    ti[v] = ti[2 * v + 2];
  }
}
void build(int v, int l, int r) {
  if (l + 1 == r) {
    t[v] = INF;
    ti[v] = l;
  } else {
    int m = (r + l) >> 1;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m, r);
    pull(v);
  }
}
void set(int v, int l, int r, int pos, int val) {
  if (l + 1 == r) {
    t[v] = val;
  } else {
    int m = (r + l) >> 1;
    if (pos < m) {
      set(2 * v + 1, l, m, pos, val);
    } else {
      set(2 * v + 2, m, r, pos, val);
    }
    pull(v);
  }
}
void query(int v, int l, int r, int ql, int qr, int &mn, int &ans) {
  if (qr <= l || r <= ql) {
    return;
  } else if (ql <= l && r <= qr) {
    if (mn > t[v]) {
      mn = t[v];
      ans = ti[v];
    }
  } else {
    int m = (r + l) >> 1;
    query(2 * v + 1, l, m, ql, qr, mn, ans);
    query(2 * v + 2, m, r, ql, qr, mn, ans);
  }
}
void init(int n_) {
  n = n_;
  build(0, 0, n);
}
void set(int pos, int val) { set(0, 0, n, pos, val); }
int query(int l, int r) {
  int mn = INF;
  int ans = -1;
  query(0, 0, n, l, r, mn, ans);
  return ans;
}
}  // namespace SegmentTree
namespace Solver {
int n;
int val[N];
vector<int> by_val[N + 1];
vector<Query> by_x[N + 1];
void init(int n_, int *val_) {
  n = n_;
  for (int i = 0; i < n; i++) {
    val[i] = val_[i];
  }
  for (int i = 0; i < n; i++) {
    val[i] = min(val[i], N);
  }
  for (int i = 0; i < n; i++) {
    by_val[val[i]].push_back(i);
  }
}
void add_query(int l, int r, int x, int *pnt_ans) {
  x = min(x, N);
  by_x[x].push_back(Query(Query(l, r, x, pnt_ans)));
}
void solve() {
  SegmentTree::init(n);
  for (int i = N; i >= 0; i--) {
    for (auto q : by_x[i]) {
      *q.pnt_ans = SegmentTree::query(q.l, q.r);
    }
    for (auto x : by_val[i]) {
      SegmentTree::set(x, val[x]);
    }
  }
}
}  // namespace Solver
int n;
int a[N];
int nxt[N];
int ind[N];
int dp[N];
int wr[N];
int rnext[N];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<int> seen;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    seen.push_back(a[i]);
  }
  sort(seen.begin(), seen.end());
  seen.resize(unique(seen.begin(), seen.end()) - seen.begin());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(seen.begin(), seen.end(), a[i]) - seen.begin();
  }
  {
    vector<int> v(n, INF);
    for (int i = n - 1; i >= 0; i--) {
      nxt[i] = v[a[i]];
      v[a[i]] = i;
    }
  }
  Solver::init(n, nxt);
  for (int i = 0; i < n; i++) {
    if (nxt[i] != INF) {
      Solver::add_query(i + 1, nxt[i], nxt[i], &ind[i]);
    } else {
      ind[i] = -1;
    }
  }
  fill(wr, wr + n + 1, -1);
  Solver::solve();
  for (int i = 0; i < n; i++) {
    rnext[i] = INF;
    if (ind[i] != -1) {
      rnext[i] = nxt[ind[i]];
    }
  }
  for (int i = 0; i < n; i++) {
    int p = i;
    for (int j = 0; j < 3; j++) {
      if (p == INF) continue;
      p = nxt[p];
    }
    if (p == INF) continue;
    if (ind[i] == -1 || nxt[ind[i]] > p) {
      ind[i] = nxt[i];
      rnext[i] = p;
    }
  }
  for (int i = 0; i < n; i++) {
    if (dp[i + 1] < dp[i]) {
      dp[i + 1] = dp[i];
      wr[i + 1] = -2;
    }
    if (ind[i] != -1 && nxt[ind[i]] != INF) {
      if (dp[rnext[i] + 1] < dp[i] + 1) {
        dp[rnext[i] + 1] = dp[i] + 1;
        wr[rnext[i] + 1] = i;
      }
    }
  }
  vector<int> ans;
  {
    int pnt = n;
    while (pnt != -1 && wr[pnt] != -1) {
      if (wr[pnt] == -2) {
        pnt--;
      } else {
        ans.push_back(wr[pnt]);
        pnt = wr[pnt];
      }
    }
    reverse(ans.begin(), ans.end());
  }
  cout << 4 * dp[n] << endl;
  for (auto t : ans) {
    cout << seen[a[t]] << ' ' << seen[a[ind[t]]] << ' ';
    cout << seen[a[t]] << ' ' << seen[a[ind[t]]] << ' ';
  }
  cout << endl;
}
