#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m,k,v[200009];
vector<pair<int,pair<int,int> > > g[200009];
pair<int,int> d[200009];
void dijkstra(int v0)
{
for(int i=1;i<=n;i++) d[i].first=0x3f3f3f3f;
priority_queue <pair<int,pair<int,int> >,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > >q;
d[0].first=0;
q.push({v0,{0,0}});
while(!q.empty())
{
auto x=q.top();
q.pop();
for(auto i:g[x.first])
{
if(d[i.first].first>x.second.first+i.second.first&&x.second.second+i.second.second<k)
{
d[i.first].first=x.second.first+i.second.first;
d[i.first].second=x.second.second+i.second.second;
q.push({i.first,{d[i.first].first,d[i.first].second}});
v[i.first]=1;
}
}
}
}
signed main()
{
int x,y,ti,h;
cin>>k>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>x>>y>>ti>>h;
g[x].push_back({y,{ti,h}});
g[y].push_back({x,{ti,h}});
}
int s,t;
cin>>s>>t;
dijkstra(s);
if(v[t]||d[t].first!=0x3f3f3f3f) cout<<d[t].first<<'\n';
else cout<<-1<<'\n';
return 0;
}