This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//******************/
//*   I<3   C++    */
//*  I WANT ANY AC */
//* I LOVE PROGRAM!*/
//*IT'S long longERESTING*/
//* I LOVE PROGRAM!*/
//*  IN CONTESTS   */
//*   GET SCORE    */
//*    AC CODE     */
//*     LET'S      */
//*      GO        */
//*  Written by:   */
//*   Duc Minh     */
#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
#include <string>
#include <queue>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <deque>
using namespace std;
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define all(a) a.begin(),a.end()
// #define push_back emplace_back
#define endl "\n"
#define all1(a) a+1,a+n+1
// #define gcd(a,b) __gcd(a,b);
// #define lcm(a,b) (a*b)/gcd(a,b);
const long long INF = (long long)1e9;
const long long MOD = (long long)1e9+7;
const long long MODD = 998244353;
const long long maxN = 2e6;
// const unsigned unsigned long long du=1000000007;
// unsigned unsigned long long w[20],v[20];
// map<unsigned unsigned long long,unsigned unsigned long long> q;
// unsigned unsigned long long n,ans=INF;
// unsigned unsigned unsigned long long a[200009],pre[200009],x=0,sum=0;
// unsigned unsigned long long n,h[1000009],j[1000009],b[1000009];
// unsigned unsigned long long t,n,x,a[1000009];
// unsigned long long t,a[10000009],max1i[10000009],maxin[10000009],maxx=-INF;
///--------------------------------
void solve();
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); 
    freopen("output.txt", "w", stdout);
    #endif
    // freopen("spath.inp", "r", stdin); 
    // freopen("spath.out", "w", stdout);
    
    
    long long t;
    // cin >> t;
    
    t=1;
    while (t--){
       
        solve();
       
    }
    
    
    cerr << "Time elapsed: " << TIME << "s.\n";
    cerr << "ducminh" << "\n";
    return 0;
}
///--------------------[PROBLEM SOLUTION]--------------------///
multiset<long long> ss;
long long sum=0,sum1=0,ans=0,sum2=0;
void solve(){
 
    
    long long k,n;
    cin >> k >> n;
    for (long long i=1; i<=n; i++){
        char ch1,ch2;
        long long x,y;
        cin >> ch1 >> x >> ch2 >> y;
        if (ch1!=ch2)  {
            ss.insert(x); ss.insert(y); 
            sum+=x+y;
        }
        else sum1+=2,sum2+=abs(y-x);
    }
    auto it=ss.begin();
    if (ss.size()==0) return cout << sum2, void();
    advance(it, ss.size()/2);
    long long tam=*it;
    
    for (long long x: ss){
 
        ans+=abs(tam-x);
    }
 
    ans+=(ss.size()/2);
    ans+=sum2;
 
    cout << ans;
    
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:76:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:77:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |