#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
cin.tie(0)->sync_with_stdio(0);
int k, n; cin >> k >> n;
vector<tuple<char,int,char,int>> people(n);
for(int i = 0; i < n; i++){
char a, b; int a1, b1;
cin >> a >> a1 >> b >> b1;
people[i] = {a, a1, b, b1};
}
// Collect preferred bridge positions from crossing citizens
vector<int> bridgePos;
int baseSum = 0;
for(auto& [p, s, q, t] : people){
if(p == q){
// Same zone, no crossing needed
baseSum += abs(s - t);
} else {
// Must cross: collect both endpoints as candidate bridge positions
bridgePos.push_back(s);
bridgePos.push_back(t);
// Without bridge: just add |s-t|+1 baseline
// We'll compute savings with bridge
}
}
// Optimal bridge is at median of bridgePos
sort(bridgePos.begin(), bridgePos.end());
long long best = LLONG_MAX;
// Try median position(s)
for(int bi = (bridgePos.size()-1)/2; bi <= bridgePos.size()/2; bi++){
int bridge = bridgePos[bi];
int total = baseSum;
for(auto& [p, s, q, t] : people){
if(p != q){
total += abs(s - bridge) + 1 + abs(t - bridge);
}
}
best = min(best, total);
}
cout << best << "\n";
}