#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define pb push_back
const int N=3e5+5;
int dist(int a, int b){
return abs(a-b);
}
signed main(){
int k,n;
cin>>k>>n;
vector<pair<int,int>>v;
int s=0;
vector<int>vc;
for(int i=1;i<=n;i++){
char x,y;
int a,b;
cin>>x>>a>>y>>b;
if(x==y){
s+=abs(a-b);
}
else{
v.pb({a,b});
vc.pb(a);
vc.pb(b);
}
}
k=1e9;
for(auto a:vc){
int g=0;
for(auto p:v){
g+=dist(a,p.ff)+dist(a,p.ss)+1;
}
k=min(k,g);
}
if(k==1e9)k=0;
cout<<s+k;
}
# | 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... |