#include <bits/stdc++.h>
using namespace std;
template <typename T>
void print_container(ostream& os, const T& c) {
  const char* _s = " ";
  if (!c.empty()) {
    __typeof__(c.begin()) last = --c.end();
    for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) {
      os << *it;
      if (it != last) os << _s;
    }
  }
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& c) {
  print_container(os, c);
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& c) {
  print_container(os, c);
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& c) {
  print_container(os, c);
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const deque<T>& c) {
  print_container(os, c);
  return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& c) {
  print_container(os, c);
  return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T>
void print(T a, int n, const string& split = " ") {
  for (int i = 0; i < n; i++) {
    cout << a[i];
    if (i + 1 != n) cout << split;
  }
  cout << endl;
}
template <typename T>
void print2d(T a, int w, int h, int width = -1, int br = 0) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (width != -1) cout.width(width);
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  while (br--) cout << endl;
}
template <typename T>
void input(T& a, int n) {
  for (int i = 0; i < n; ++i) cin >> a[i];
}
template <typename T, typename U>
void upmin(T& a, const U& b) {
  a = min<T>(a, b);
}
template <typename T, typename U>
void upmax(T& a, const U& b) {
  a = max<T>(a, b);
}
template <typename T>
void uniq(T& a) {
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
}
template <class T>
string to_s(const T& a) {
  ostringstream os;
  os << a;
  return os.str();
}
template <class T>
T to_T(const string& s) {
  istringstream is(s);
  T res;
  is >> res;
  return res;
}
void fast_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
bool in_rect(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
template <typename T>
class RMQ {
 private:
  vector<T> data;
  int n;
  const T UNDEF;
  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return UNDEF;
    if (a <= l && r <= b)
      return data[k];
    else {
      T left = _query(a, b, 2 * k + 1, l, (l + r) / 2);
      T right = _query(a, b, 2 * k + 2, (l + r) / 2, r);
      return min(left, right);
    }
  }

 public:
  RMQ(int n, T UNDEF) : UNDEF(UNDEF) {
    this->n = 1;
    while (this->n < n) this->n *= 2;
    data = vector<T>(this->n * 2, UNDEF);
  }
  void update(int k, T a) {
    k += n - 1;
    data[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = min(data[2 * k + 1], data[2 * k + 2]);
    }
  }
  T query(int a, int b) { return _query(a, b, 0, 0, n); }
};
vector<int> dp, pre, other;
void update(int i, int val, int from, int oth) {
  if (val > dp[i]) {
    dp[i] = val;
    pre[i] = from;
    other[i] = oth;
  }
}
int main() {
  fast_io();
  int n;
  cin >> n;
  vector<int> a(n);
  input(a, n);
  vector<int> next(n, -1);
  {
    map<int, int> nn;
    for (int i = n - 1; i >= 0; --i) {
      if (nn.count(a[i])) next[i] = nn[a[i]];
      nn[a[i]] = i;
    }
  }
  const int inf = ((long long)(1e7));
  RMQ<pair<int, int> > rmq(n + 10, pair<int, int>(inf, -1));
  dp.assign(n + 1, 0);
  pre.assign(n + 1, -1);
  other.assign(n + 1, -1);
  for (int i = 0; i < (int)(n); ++i) {
    update(i + 1, dp[i], pre[i], other[i]);
    {
      int j = i;
      for (int _ = 0; _ < (int)(3); ++_)
        if (j != -1) j = next[j];
      if (j != -1) update(j + 1, dp[i] + 1, i, j);
    }
    if (next[i] != -1) {
      pair<int, int> best = rmq.query(i + 1, next[i]);
      if (best.second != -1) {
        int p = best.second;
        update(next[i] + 1, dp[p] + 1, p, i);
      }
      rmq.update(next[i], pair<int, int>(-dp[i], i));
    }
  }
  vector<int> seq;
  for (int i = n; pre[i] >= 0;) {
    int p = pre[i];
    int q = other[i];
    seq.push_back(a[q]);
    seq.push_back(a[p]);
    seq.push_back(a[q]);
    seq.push_back(a[p]);
    i = pre[i];
  }
  reverse((seq).begin(), (seq).end());
  cout << seq.size() << endl;
  cout << seq << endl;
}
