1 #include<cstdio>
2 #include<iostream>
3 #include<string>
4 #include<cstring>
5 #include<map>
6 using namespace std;
7 const int N=2000;
8 map<string,int> MP;
9 float R,x[N],y[N];
10 int G[N*2][N*2],n,p,a,b,l[N*2],r[N*2],slack[N*2],con[N*2];
11 bool vy[N*2],vx[N*2];
12 inline bool on(int a,int b,int c)
13 {
14 if(x[a]==x[b]&&x[b]==x[c]) return (min(y[a],y[b])<=y[c]&&y[c]<=max(y[a],y[b]));
15 return ((y[a]-y[b])*(x[c]-x[b])==(y[c]-y[b])*(x[a]-x[b]));
16 }
17 inline bool Judge(int a,int b)
18 {
19 if(x[a]>x[b]) swap(a,b);
20 for(int i=1;i<=n*2;i++) if(i!=a&&i!=b&&x[a]<=x[i]&&x[i]<=x[b])
21 if(on(a,b,i)) return false;
22 return true;
23 }
24 inline float dis(int a,int b) {return (x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]);}
25 inline void Preoperate()
26 {
27 memset(l,0,sizeof(l));
28 for(int i=1;i<=2*n;i++)
29 for(int j=1;j<=2*n;j++) l[i]=max(l[i],G[i][j]);
30 memset(r,0,sizeof(r));
31 }
32 inline bool find(int x)
33 {
34 vx[x]=1;
35 for(int i=n+1;i<=2*n;i++) if(!vy[i])
36 {
37 if(G[x][i]==l[x]+r[i])
38 {
39 vy[i]=1;
40 if(con[i]==-1||find(con[i]))
41 {
42 con[i]=x;
43 return true;
44 }
45 }
46 else slack[i]=min(slack[i],l[x]+r[i]-G[x][i]);
47 }
48 return false;
49 }
50 int main()
51 {
52 memset(G,-0x3f,sizeof(G));
53 string str;
54 scanf("%f%d",&R,&n);
55 for(int i=1;i<=2*n;i++)
56 {
57 scanf("%f %f",&x[i],&y[i]);
58 cin>>str;
59 for(int j=0;j<str.length();j++) if(str[j]<=‘Z‘&&str[j]>=‘A‘) str[j]+=‘a‘-‘A‘;
60 MP[str]=i;
61 }
62 while(1)
63 {
64 cin>>str;
65 if(str=="End") break;
66 for(int i=0;i<str.length();i++) if(str[i]<=‘Z‘&&str[i]>=‘A‘) str[i]+=‘a‘-‘A‘;
67 a=MP[str];
68 cin>>str;
69 for(int i=0;i<str.length();i++) if(str[i]<=‘Z‘&&str[i]>=‘A‘) str[i]+=‘a‘-‘A‘;
70 b=MP[str];
71 scanf("%d",&p);
72 if(a>b) swap(a,b);
73 if(dis(a,b)<=R*R&&Judge(a,b)) G[a][b]=p;
74 }
75 for(int i=1;i<=n;i++)
76 for(int j=n+1;j<=2*n;j++) if(G[i][j]<0&&dis(i,j)<=R*R&&Judge(i,j)) G[i][j]=1;
77 Preoperate();
78 memset(con,-1,sizeof(con));
79 for(int j=1;j<=n;j++)
80 {
81 for(int k=1;k<=1000;k++)
82 {
83 memset(slack,127,sizeof(slack));
84 memset(vx,0,sizeof(vx));
85 memset(vy,0,sizeof(vy));
86 if(find(j)) break;
87 int d=2e9;
88 for(int i=n+1;i<=n*2;i++) if(!vy[i]) d=min(d,slack[i]);
89 for(int i=1;i<=n;i++) if(vx[i]) l[i]-=d;
90 for(int i=n+1;i<=n*2;i++) if(vy[i]) r[i]+=d;
91 }
92 }
93 int ans=0;
94 for(int i=1;i<=n*2;i++) if(con[i]!=-1) ans+=G[con[i]][i];
95 printf("%d\n",ans);
96 return 0;
97 }