This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
struct segment
{
    int l, r, par, type;
};
bool operator<(segment a, segment b)
{
    if (a.l == b.l)
        return a.r < b.r;
    return a.l < b.l;
}
void remove(auto pos, set<segment> &Intervals, int &impair, vector<int> &parity)
{
    if (((*pos).r - (*pos).l + 1) % 2)
        impair--;
    parity[(*pos).par]--;
    pos = Intervals.erase(pos);
}
void add(segment arete, int &impair, vector<int> &parity, set<segment> &Intervals)
{
    if ((arete.r - arete.l + 1) % 2 == 1)
        impair++;
    parity[arete.par % 2]++;
    Intervals.insert({arete.l, arete.r, arete.par % 2});
}
int N;
int next(int i) { return (i + 1) % N; }
int main()
{
    auto comp = [&](segment a, segment b)
    {
        return a.par < b.par;
    };
    int M;
    cin >> N >> M;
    vector<segment> arete;
    vector<int> X(N), Y(N);
    for (int i = 0; i < N; i++)
    {
        cin >> X[i] >> Y[i];
    }
    for (int i = 0; i < N; i++)
    {
        if (X[next(i)] == X[i])
        {
            if (Y[i] < Y[next(i)])
                arete.push_back({min(Y[i], Y[next(i)]), max(Y[i], Y[next(i)]) - 1, X[i], 1});
            else
                arete.push_back({min(Y[i], Y[next(i)]), max(Y[i], Y[next(i)]) - 1, X[i], -1});
        }
    }
    sort(arete.begin(), arete.end(), comp);
    if (arete[0].type == -1)
        for (int i = 0; i < arete.size(); i++)
            arete[i].type = 0 - arete[i].type;
    int last = -1;
    int impair = 0;
    vector<int> parity(2);
    int maxx = 0;
    set<segment> Intervals;
    for (int i = 0; i < arete.size(); i++)
    {
        if (arete[i].par != last || i == arete.size() - 1)
        {
            last = arete[i].par;
            if (impair)
                break;
            if (parity[0] == 0 || parity[1] == 0)
            {
                int par = ((parity[1]) ? 1 : 0);
                maxx = ((arete[i].par % 2 == par) ? arete[i].par : (arete[i].par - 1));
            }
        }
        if (arete[i].type == 1)
        {
            auto pos = Intervals.lower_bound({arete[i].l});
            if (pos != Intervals.end() && (*pos).l == arete[i].r + 1 && (*pos).par == (arete[i].par % 2))
            {
                arete[i].r = (*pos).r;
                remove(pos, Intervals, impair, parity);
            }
            if (pos != Intervals.begin())
            {
                pos--;
                if ((*pos).r == arete[i].l - 1 && (*pos).par == (arete[i].par % 2))
                {
                    arete[i].l = (*pos).l;
                    remove(pos, Intervals, impair, parity);
                }
            }
            add(arete[i], impair, parity, Intervals);
        }
        else
        {
            auto pos = Intervals.lower_bound({arete[i].r + 1});
            pos--;
            if ((arete[i].par % 2) != (*pos).par)
                break;
            if (arete[i].l < (*pos).l)
                break;
            int left = (*pos).l, right = arete[i].l - 1;
            if (right >= left)
            {
                add({left, right, (*pos).par}, impair, parity, Intervals);
            }
            left = arete[i].r + 1, right = (*pos).r;
            if (right >= left)
            {
                add({left, right, (*pos).par}, impair, parity, Intervals);
            }
            remove(pos, Intervals, impair, parity);
        }
    }
    cout << maxx;
}
Compilation message (stderr)
Main.cpp:14:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   14 | void remove(auto pos, set<segment> &Intervals, int &impair, vector<int> &parity)
      |             ^~~~
Main.cpp: In function 'int main()':
Main.cpp:56:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<segment>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |         for (int i = 0; i < arete.size(); i++)
      |                         ~~^~~~~~~~~~~~~~
Main.cpp:63:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<segment>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     for (int i = 0; i < arete.size(); i++)
      |                     ~~^~~~~~~~~~~~~~
Main.cpp:65:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<segment>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |         if (arete[i].par != last || i == arete.size() - 1)
      |                                     ~~^~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |