//Accepted 15880 KB 250 ms
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
;
;
;
int dp[imax_n][imax_n];
int addcost[imax_m];
int deletecost[imax_m];
int n;
int m;
char s[imax_n];
int min(int a,int b)
{
return a<b?a:b;
}
void Dp()
{
memset(dp,,sizeof(dp));
//for (int i=1;i<=n;i++)
//dp[i][i]=0;
;l<=n;l++)
{
;i<=n;i++)
{
;
dp[i][j]=inf;
]==s[j-]) dp[i][j]=dp[i+][j-];
dp[i][j]=min(dp[i][j],dp[i+][j]+addcost[s[i-]-'a']);
dp[i][j]=min(dp[i][j],dp[i+][j]+deletecost[s[i-]-'a']);
dp[i][j]=min(dp[i][j],dp[i][j-]+addcost[s[j-]-'a']);
dp[i][j]=min(dp[i][j],dp[i][j-]+deletecost[s[j-]-'a']);
}
}
printf(][n]);
}
int main()
{
//while (scanf("%d%d",&m,&n)!=EOF)
{
scanf("%d%d",&m,&n);
scanf("%s",s);
];
memset(addcost,,sizeof(addcost));
memset(deletecost,,sizeof(deletecost));
;i<=m;i++)
{
scanf("%s",ss);
scanf(]-]-'a']);
}
Dp();
}
;
}