#include <queue>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int MAXN = 105;
const int MAXE = 10005;
const int MAX_COST = 10005;
const int inf = 0x3f3f3f3f;
typedef struct _node
{
int v, next;
int l, c;
}N;
N edge[MAXE];
int cntEdge, head[MAXN];
typedef struct _no
{
int v;
int c, dis;
_no(): dis(inf) {}
_no(int a, int b, int c1): v(a), dis(b), c(c1) {}
friend bool operator < (const struct _no &n1, const struct _no &n2)
{
return n1.dis > n2.dis;
}
}priN;
void init()
{
cntEdge = 0;
for(int i = 0; i < MAXN; i++)
head[i] = -1;
}
void addEdge(int u, int v, int l, int c)
{
edge[cntEdge].v = v;
edge[cntEdge].l = l;
edge[cntEdge].c = c;
edge[cntEdge].next = head[u];
head[u] = cntEdge++;
}
int dis[MAXN];
int dijkstra(int s, int n, int tol)//1是起点,n是终点
{
int vst[MAXN] = {0};
for(int i = 0; i <= n; i++)
dis[i] = inf;
priority_queue<priN> Q;
Q.push(priN(s, 0, 0));
//dis[s] = 0;
while(!Q.empty())
{
priN pre = Q.top();
Q.pop();
if(pre.v == n)
return pre.dis;
for(int f = head[pre.v]; f != -1; f = edge[f].next)
{
int son = edge[f].v;
int l = edge[f].l;
int c = edge[f].c;
if(pre.c + c <= tol)
{
//dis[son] = dis[pre.v] + l;
Q.push(priN(son, pre.dis + l, pre.c + c));
}
}
}
return -1;
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n, tol;
while(scanf("%d", &tol) == 1)
{
int m;
scanf("%d %d", &n, &m);
init();
int u, v, l, c;
for(int i = 0; i < m; i++)
{
scanf("%d %d %d %d", &u, &v, &l, &c);
addEdge(u, v, l, c);
}
int sol = dijkstra(1, n, tol);
printf("%d\n", sol);
}
return 0;
}