#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ins insert
//cout<<fixed<<setprecision(3); 3 decimalke brez fixed pa 3 zanesljiva mesta
const int MAXN=1e6+3;
const long long linf=1e18;
const int inf=1e9;
const int mod=1e9+7;
void solve(){
int k,n;cin>>k>>n;
ll res=0;
vector<pair<int,int>>house;
vector<int>pos;
for(int i=0;i<n;i++){
char t,tt;int p,pp;
cin>>t>>p>>tt>>pp;
if(tt==t){
res+=abs(pp-p);
}
else{
res++;
pos.pb(p);pos.pb(pp);
house.pb({p,pp});
}
}
if(k==1){
cout<<-1<<endl;
}
else{
ll mn=1e18;
for(int i=0;i<int(pos.size());i++){
for(int j=i+1;j<int(pos.size());j++){
ll m=res;
for(int ki=0;ki<int(house.size());ki++){
ll e=abs(house[ki].first-pos[i])+abs(house[ki].second-pos[i]);
ll ee=abs(house[ki].first-pos[j])+abs(house[ki].second-pos[j]);
m+=min(e,ee);
}
mn=min(m,mn);
}
}
cout<<mn<<endl;
}
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
int t=1;
while(t--) solve();
}
# | 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... |