#include using namespace std; #include "atcoder/segtree.hpp" struct S { int x22; int x20; int x00; int le; }; S op(S l, S r) { S ret; ret.x22 = l.x22 + r.x22; ret.x20 = min({l.x22 + r.x20, l.x22 + r.x00, l.x20 + r.x00}); ret.x00 = l.x00 + r.x00; ret.le = l.le + r.le; return ret; } S e() { return {0, 0, 0, 0}; } int main() { int n; cin >> n; assert(1 <= n and n <= 300000); vector> pos(n); for (int i = 0; i < n; i++) { int a; cin >> a; assert(1 <= a and a <= n); pos[a - 1].push_back(i); } S e2 = {0, 0, 1, 1}; S e0 = {1, 0, 0, 1}; vector ini(n, e2); atcoder::segtree seg(ini); int ans = 1 << 30; for (int i = 0; i < n; i++) { if (pos[i].empty()) continue; int tot = 0; for (int j = 0; j < pos[i].size() - 1; j++) { int l = pos[i][j] + 1; int r = pos[i][j + 1]; auto res = seg.prod(l, r); tot += min({res.x22, res.x20, res.x00}) + res.le; } { auto res1 = seg.prod(pos[i].back() + 1, n); auto res2 = seg.prod(0, pos[i].front()); auto res = op(res1, res2); tot += min({res.x22, res.x20, res.x00}) + res.le; } ans = min(ans, tot); for (auto p : pos[i]) { seg.set(p, e0); } } cout << ans << endl; }