#include<bits/stdc++.h>
using namespace std;
int city[21][21],n,load[21][21];
int power(int n)
{
    int num=1;
    for(int i=0;i<n;i++)num*=10;
    return num;
}
int aserch(int endp,int now,int befor)
{
    int money=50000;
    if(city[now][endp]!=-1)return city[now][endp];
    else{
        for(int i=1;i<=n;i++){
                if(city[now][i]!=-1&&i!=befor&&load[now][i]!=10){
                    load[now][i]++;
                    money=min(aserch(endp,i,now)+city[now][i],money);
                }
        }
    }
    return money;
}
int bserch(int stap,int now,int befor)
{
    int money=50000,num;
    if(city[now][stap]!=-1){
        return city[now][stap];
    }
    else{
        for(int i=1;i<=n;i++){
            if(city[now][i]!=-1&&i!=befor&&load[now][i]!=20){
                    load[now][i]++;
                   num=bserch(stap,i,now)+city[now][i];
                   if(num>-1)money=min(num,money);
            }
        }
    }
    return money;

}
int main()
{
    int m;
    cin>>n>>m;
    memset(city,-1,sizeof(city));
    memset(load,0,sizeof(load));
    string str;
    for(int i=0;i<m;i++){
        int kawa[2]={};
        cin>>str;
        for(int j=0,co=0,point=0;;j++){
            if(str[j]==','||str[j]=='\0'){
                int mo=0;
                if(co<2){
                    for(int k=point;k<j;k++)kawa[co]+=(str[k]-'0')*power(j-k-1);
                    point=j+1;
                    co++;
                }
                else if(co==2){
                    for(int k=point;k<j;k++)mo+=(str[k]-'0')*power(j-k-1);
                    city[kawa[0]][kawa[1]]=mo;
                    point=j+1;
                    co++;
                }
                else{
                    for(int k=point;k<j;k++)mo+=(str[k]-'0')*power(j-k-1);
                    city[kawa[1]][kawa[0]]=mo;
                    break;
                }
            }
        }
    }
    cin>>str;
    int pointing[3]={},Pillar=0;
    for(int i=0,co=0,point=0;;i++){
        if(str[i]==','){
            for(int j=point;j<i;j++)pointing[co]+=(str[j]-'0')*power(i-j-1);
            point=i+1;
            co++;
        }
        if(str[i]=='\0'){
            for(int j=point;j<i;j++)Pillar+=(str[j]-'0')*power(i-j-1);
            break;
        }
    }
    cout<<pointing[2]-Pillar-aserch(pointing[1],pointing[0],0)-bserch(pointing[0],pointing[1],0)<<endl;
}