import heapq
class Edge:
def __init__(self, to, weight):
self.to = to
self.weight = weight
def __lt__(self, other):
return self.weight < other.weight
maxn = 10003
inf = 1 << 29
N, M = map(int, input().split())
va = [[] for _ in range(maxn)]
vb = [[] for _ in range(maxn)]
v = [[] for _ in range(maxn)]
dist = [[inf] * maxn for _ in range(3)]
def dij(v, a, src):
dist[a] = [inf] * maxn
dist[a][src] = 0
pq = []
heapq.heappush(pq, Edge(src, 0))
while pq:
edge = heapq.heappop(pq)
cur = edge.to
dst = edge.weight
if dst != dist[a][cur]:
continue
for next_edge in v[cur]:
nxt = next_edge.to
c = next_edge.weight + dist[a][cur]
if c < dist[a][nxt]:
dist[a][nxt] = c
heapq.heappush(pq, Edge(nxt, dist[a][nxt]))
return dist[a][N - 1]
for i in range(M):
a, b, p, q = map(int, input().split())
a -= 1
b -= 1
va[b].append(Edge(a, p))
vb[b].append(Edge(a, q))
dij(va, 0, N - 1)
dij(vb, 1, N - 1)
for cur in range(N):
for j in range(len(va[cur])):
nxt = va[cur][j].to
c = 0
dst1 = va[cur][j].weight
dst2 = vb[cur][j].weight
if dist[0][nxt] - dist[0][cur] != dst1:
c += 1
if dist[1][nxt] - dist[1][cur] != dst2:
c += 1
v[nxt].append(Edge(cur, c))
ans = dij(v, 2, 0)
print(ans)