Language: C++

#include <iostream>
#include <sstream>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <cstdint>
#include <cmath>
#include <bitset>
#include <string>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>

#define N_TIMES(i, n)     for ( uint64_t i = 0;     i <  n; ++i )
#define N_TIMES_REV(i, n) for (  int64_t i = n - 1; i >= 0; --i )

#ifdef __DEBUG__

template<typename T, typename U>
std::ostream& operator<<(std::ostream &os, const std::pair<T, U> &pair)
{
  os << pair.first << ": " << pair.second;
  return os;
}

template<class ForwardIterator>
std::ostream& _iterator_to_display(
  std::ostream &os, 
  ForwardIterator begin, ForwardIterator end, 
  const std::string &delim = ", "
)
{
  auto itr = begin;
  while (itr != end) {
    os << *itr;
    ++itr;
    if (itr != end) os << delim;
  }
  return os;
}

template<typename T>
std::ostream& operator<<(std::ostream &os, const std::list<T> &list)
{
  os << '[';
  _iterator_to_display(os, list.begin(), list.end());
  os << ']';
  return os;
}

template<typename T>
std::ostream& operator<<(std::ostream &os, const std::vector<T> &vector)
{
  os << '[';
  _iterator_to_display(os, vector.begin(), vector.end());
  os << ']';
  return os;
}

template<typename T, class C=std::less<T>>
std::ostream& operator<<(std::ostream &os, const std::set<T> &set)
{
  os << '{';
  _iterator_to_display(os, set.begin(), set.end());
  os << '}';
  return os;
}

template<typename T, class C=std::less<T>>
std::ostream& operator<<(std::ostream &os, const std::multiset<T> &set)
{
  os << '{';
  _iterator_to_display(os, set.begin(), set.end());
  os << '}';
  return os;
}

template<typename T, typename U, class C=std::less<T>>
std::ostream& operator<<(std::ostream &os, const std::map<T, U> &map)
{
  os << '{';
  _iterator_to_display(os, map.begin(), map.end());
  os << '}';
  return os;
}

template<typename T, typename U, class C=std::less<T>>
std::ostream& operator<<(std::ostream &os, const std::multimap<T, U> &map)
{
  os << '{';
  _iterator_to_display(os, map.begin(), map.end());
  os << '}';
  return os;
}

#define debug(var) std::cerr << #var" = " << var << std::endl

#endif /* __DEBUG__ */

bool compare(const std::pair<uint64_t, uint64_t> &a, const std::pair<uint64_t, uint64_t> &b)
{
  return (a.second == b.second) ? a.first < b.first : a.second < b.second;
}

int main(int argc, char **argv)
{
  uint64_t N;
  std::cin >> N;
  
  std::vector<uint64_t> v(N);
  N_TIMES(n, N) { std::cin >> v[n]; }
  
  std::map<uint64_t, uint64_t> count_odd, count_even;
  for (uint64_t n = 0; n < N; ++n) {
    if (n % 2 == 0) {
      auto itr = count_odd.find(v[n]);
      if (itr == count_odd.end()) {
        count_odd.emplace(v[n], 1);
      } else {
        ++count_odd[ v[n] ];
      }
    } else {
      auto itr = count_even.find(v[n]);
      if (itr == count_even.end()) {
        count_even.emplace(v[n], 1);
      } else {
        ++count_even[ v[n] ];
      }
    }
  }
  debug(count_odd); debug(count_even);
  
  auto itr_odd  = std::max_element(count_odd.begin(),  count_odd.end(),  compare);
  auto itr_even = std::max_element(count_even.begin(), count_even.end(), compare);
  
  if (itr_odd->first == itr_even->first) {
    if (itr_odd->second == itr_even->second) {
      uint64_t P = itr_odd->second;
      
      count_odd.erase(itr_odd);
      count_even.erase(itr_even);
      
      itr_odd  = std::max_element(count_odd.begin(),  count_odd.end(),  compare);
      itr_even = std::max_element(count_even.begin(), count_even.end(), compare);
      
      uint64_t M = 0;
      if (itr_odd->second < itr_even->second) {
        M = ((N >> 1) - P) + ((N >> 1) - itr_even->second);
      } else {
        M = ((N >> 1) - itr_odd->second) + ((N >> 1) - P);
      }
      std::cout << M << std::endl;
    } else {
      if (itr_odd->second < itr_even->second) {
        count_odd.erase(itr_odd);
        itr_odd  = std::max_element(count_odd.begin(),  count_odd.end(),  compare);
      } else {
        count_even.erase(itr_even);
        itr_even = std::max_element(count_even.begin(), count_even.end(), compare);
      }
      
      uint64_t num_odd  = (itr_odd  == count_odd.end())  ? 0 : itr_odd->second;
      uint64_t num_even = (itr_even == count_even.end()) ? 0 : itr_even->second;
      uint64_t M = ((N >> 1) - num_odd) + ((N >> 1) - num_even);
      std::cout << M << std::endl;
    }
  } else {
    uint64_t num_odd  = (itr_odd  == count_odd.end())  ? 0 : itr_odd->second;
    uint64_t num_even = (itr_even == count_even.end()) ? 0 : itr_even->second;
    uint64_t M = ((N >> 1) - num_odd) + ((N >> 1) - num_even);
    std::cout << M << std::endl;
  }
  
  return 0;
}

Compiler output:
a.cc: In function 'int main(int, char**)':
a.cc:135:3: error: 'debug' was not declared in this scope
  135 |   debug(count_odd); debug(count_even);
      |   ^~~~~