#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector<pair<ll,ll>> st;
const int N=1e5+10;
ll pre[N],cand=0;
priority_queue<ll> ql;
priority_queue<ll,vector<ll>,greater<ll>> qr;
//median is at the top of ql
void ins(ll x){
if(ql.empty() && qr.empty()){
ql.push(x);
return;
}
if(x>ql.top()){
qr.push(x);
cand+=x-ql.top();
if((int)qr.size()>(int)ql.size()-1){
ql.push(qr.top());
qr.pop();
}
}
else{
cand+=ql.top()-x;
ql.push(x);
if((int)ql.size()-2>qr.size()){
ll out=ql.top();
qr.push(ql.top());
ql.pop();
cand-=out-ql.top();
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,k; cin>>k >>n;
ll base=0;
for(int i=1;i<=n;i++){
char ca,cb; ll u,v; cin>>ca >>u >>cb >>v;
if(ca==cb) base+=abs(u-v);
else st.push_back({min(u,v),max(u,v)});
}
sort(st.begin(),st.end(),[&](pair<ll,ll> a,pair<ll,ll> b){
return a.first+a.second<b.first+b.second;
});
int sz=st.size();
for(int i=0;i<sz;i++){
ins(st[i].first);
ins(st[i].second);
pre[i]=cand;
}
ll ret=cand+base+(ll)sz;
if(k==1){
cout<<pre[sz-1]+base+(ll)sz <<"\n";
return 0;
}
while(!ql.empty()) ql.pop();
while(!qr.empty()) qr.pop();
cand=0;
for(int i=sz-1;i>=1;i--){
ins(st[i].first);
ins(st[i].second);
ret=min(ret,pre[i-1]+cand+base+(ll)sz);
}
cout<<ret <<"\n";
}