// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <vector>
#include <iomanip>
#include <string>
#include <queue>
#include <set>
#include <deque>
using namespace std;
#define int long long
#define endl "\n"
#define fi first
#define se second
const int M=1e9+7;
const int inf = 1e14;
const int LOG=17;
const int N=2e5+5;
int n , m , c , w , k , t=1 , q=1 , x , y , z , l , r;
bool comp(pair<int,int>x,pair<int,int>y){
return (x.fi+x.se)/2<(y.fi+y.se)/2;
}
void solve(){
cin >> k >> n;
vector<pair<int,int>>a;
int co=0;
for (int i=0;i<n;i++){
char s,p;
cin >> s >> x >> p >> y;
if (s==p){
co+=abs(x-y);
}
else{
co+=1;
a.push_back({x,y});
}
}
n=a.size();
if (n==0){
cout << co << endl;
return;
}
sort(a.begin(),a.end(),comp);
vector<int>pr(n);
priority_queue<int>l;
priority_queue<int,vector<int>,greater<int>>r;
int l1=0,r1=0;
for (int i=0;i<n;i++){
x=a[i].fi,y=a[i].se;
if (x>y)swap(x,y);
l.push(x);l1+=x;
l.push(y);l1+=y;
r.push(l.top());r1+=l.top();
l1-=l.top();l.pop();
r.push(l.top());r1+=l.top();
l1-=l.top();l.pop();
l.push(r.top());l1+=r.top();
r1-=r.top();r.pop();
int m=l.top();
pr[i]=(m*(i+1)-l1)+(r1-m*(i+1));
}
vector<int>sf(n);
while (l.size())l.pop();
while (r.size())r.pop();
l1=0,r1=0;
for (int i=n-1;i>=0;i--){
x=a[i].fi,y=a[i].se;
if (x>y)swap(x,y);
l.push(x);l1+=x;
l.push(y);l1+=y;
r.push(l.top());r1+=l.top();
l1-=l.top();l.pop();
r.push(l.top());r1+=l.top();
l1-=l.top();l.pop();
l.push(r.top());l1+=r.top();
r1-=r.top();r.pop();
int m=l.top();
sf[i]=(m*(n-i)-l1)+(r1-m*(n-i));
}
int ans=pr[n-1];
if (k==2){
for (int i=0;i<n-1;i++){
ans=min(ans,pr[i]+sf[i+1]);
}
}
cout << co+ans << endl;
}
signed main()
{
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);//DO NOT USE IN INTERACTIVE
cout << fixed << setprecision(4);
srand(time(0));
// int t=1;
// cin >> t;
for (int _=1;_<=t;_++){
solve();
q++;
}
}