#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
typedef pair<int, int> pii;
vector<int> points;
long long easy;
long long ans;
long long lt[110000];
long long rt[110000];
int n, k;
vector< pii > bridge;
bool compara(pii a, pii b) {
return a.first + a.second < b.first + b.second;
}
template<class T>
void calc(T it, T ed, long long *arr) {
multiset<int> sm;
multiset<int> bg;
long long ssm = 0;
long long sbg = 0;
int c = 0;
arr[c++] = 0;
while (it != ed) {
sm.insert(it->first);
ssm += it->first;
bg.insert(it->second);
sbg += it->second;
while (*sm.rbegin() > *bg.begin()) {
sbg += *sm.rbegin() - *bg.begin();
ssm += *bg.begin() - *sm.rbegin();
bg.insert(*sm.rbegin());
sm.insert(*bg.begin());
bg.erase(bg.begin());
sm.erase(--sm.end());
}
arr[c++] = sbg - ssm;
it++;
}
}
int main() {
scanf("%d %d", &k, &n);
for (int i = 0; i < n; i++) {
char t1, t2;
int x, y;
scanf(" %c %d %c %d", &t1, &x, &t2, &y);
if (t1 == t2) easy += abs(y-x);
else bridge.push_back(mp(x, y));
}
sort(bridge.begin(), bridge.end(), compara);
calc(bridge.begin(), bridge.end(), lt); // prefixes
calc(bridge.rbegin(), bridge.rend(), rt); // suffixes
n = bridge.size();
if (k == 1) {
ans = rt[n];
}
else {
ans = 1000000000000000000LL;
for (int in_lt = 0; in_lt <= n; in_lt++) {
ans = min(ans, lt[in_lt] + rt[n - in_lt]);
}
}
printf("%lld\n", easy + ans + n);
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOyAKCiNkZWZpbmUgbXAgbWFrZV9wYWlyCnR5cGVkZWYgcGFpcjxpbnQsIGludD4gcGlpOwoKdmVjdG9yPGludD4gcG9pbnRzOwpsb25nIGxvbmcgZWFzeTsKbG9uZyBsb25nIGFuczsKbG9uZyBsb25nIGx0WzExMDAwMF07CmxvbmcgbG9uZyBydFsxMTAwMDBdOwppbnQgbiwgazsKdmVjdG9yPCBwaWkgPiBicmlkZ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