#include <climits>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

struct Node {
    int left;
    int right;
    bool flag;
    Node() : left(-1), right(-1), flag(true) {}
    Node(int l, int r) : left(l), right(r), flag(true) {}
};

int solve(vector<int> &x) {
    int count = 0;
    vector<Node> nodes(x.size());
    priority_queue<pair<int, int>> pq;
    for (int i = 0; i < x.size(); i++) {
        if (i > 0 && x[i] == x[i - 1] + 1) {
            pq.push(make_pair(x[i], i));
        } else if (i < x.size() - 1 && x[i] == x[i + 1] + 1) {
            pq.push(make_pair(x[i], i));
        }
        nodes[i].left = i > 0 ? i - 1 : -1;
        nodes[i].right = i < x.size() - 1 ? i + 1 : -1;
        nodes[i].flag = true;
    }

    while (!pq.empty()) {
        pair<int, int> n = pq.top();
        pq.pop();

        if (nodes[n.second].flag &&
            (nodes[n.second].left >= 0 &&
                 x[nodes[n.second].left] == n.first - 1 ||
             nodes[n.second].right >= 0 &&
                 x[nodes[n.second].right] == n.first - 1)) {
            count++;
            nodes[n.second].flag = false;
            if (nodes[n.second].left >= 0) {
                nodes[nodes[n.second].left].right = nodes[n.second].right;
            }
            if (nodes[n.second].right >= 0) {
                nodes[nodes[n.second].right].left = nodes[n.second].left;
            }
            if (nodes[n.second].left >= 0 && nodes[n.second].right) {
                if (x[nodes[n.second].left] == x[nodes[n.second].right] + 1) {
                    pq.push(make_pair(x[nodes[n.second].left],
                                      nodes[n.second].left));
                } else if (x[nodes[n.second].right] ==
                           x[nodes[n.second].left] + 1) {
                    pq.push(make_pair(x[nodes[n.second].right],
                                      nodes[n.second].right));
                }
            }
        }
    }
    return count;
}

int main(void) {
    vector<int> in = {1, 3, 5, 7, 9, 8, 6, 4, 2};
    cout << solve(in);
    return 0;
}