using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("2 1");
WillReturn.Add("1 2 2 3");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("2 3");
WillReturn.Add("1 2 2 3");
WillReturn.Add("1 2 2 1");
WillReturn.Add("1 1 1 1");
//3
}
else if (InputPattern == "Input3") {
WillReturn.Add("4 2");
WillReturn.Add("1 2 3 4");
WillReturn.Add("3 4 5 6");
//-1
}
else if (InputPattern == "Input4") {
WillReturn.Add("6 9");
WillReturn.Add("1 1 0 0");
WillReturn.Add("1 3 1 2");
WillReturn.Add("1 5 2 3");
WillReturn.Add("5 2 16 5");
WillReturn.Add("2 6 1 10");
WillReturn.Add("3 4 3 4");
WillReturn.Add("3 5 3 10");
WillReturn.Add("5 6 1 100");
WillReturn.Add("4 2 0 110");
//20
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long mN;
struct EdgeInfoDef
{
internal long ToNode;
internal long C;
internal long D;
}
static Dictionary<long, List<EdgeInfoDef>> mEdgeInfoListDict = new Dictionary<long, List<EdgeInfoDef>>();
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
SplitAct(InputList[0]);
mN = wkArr[0];
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
long FromNode = wkArr[0];
long ToNode = wkArr[1];
long C = wkArr[2];
long D = wkArr[3];
// 自己ループは除外
if (FromNode == ToNode) continue;
if (mEdgeInfoListDict.ContainsKey(FromNode) == false) {
mEdgeInfoListDict[FromNode] = new List<EdgeInfoDef>();
}
if (mEdgeInfoListDict.ContainsKey(ToNode) == false) {
mEdgeInfoListDict[ToNode] = new List<EdgeInfoDef>();
}
EdgeInfoDef WillAdd1;
WillAdd1.ToNode = ToNode;
WillAdd1.C = C;
WillAdd1.D = D;
mEdgeInfoListDict[FromNode].Add(WillAdd1);
EdgeInfoDef WillAdd2;
WillAdd2.ToNode = FromNode;
WillAdd2.C = C;
WillAdd2.D = D;
mEdgeInfoListDict[ToNode].Add(WillAdd2);
}
Dijkstra();
}
//ダイクストラ法で、各ノードまでの最短距離を求める
static void Dijkstra()
{
var InsPQueue = new PQueue();
// 距離合計[確定ノード]なDict
var KakuteiNodeDict = new Dictionary<long, long>();
KakuteiNodeDict[1] = 0;
//Enqueue処理
Action<long> EnqueueAct = pFromNode =>
{
if (mEdgeInfoListDict.ContainsKey(pFromNode) == false) {
return;
}
foreach (EdgeInfoDef EachEdge in mEdgeInfoListDict[pFromNode]) {
// 確定ノードならContinue
if (KakuteiNodeDict.ContainsKey(EachEdge.ToNode)) continue;
long wkSumTime = KakuteiNodeDict[pFromNode] +
DeriveMinTime(EachEdge.C, EachEdge.D, KakuteiNodeDict[pFromNode]);
PQueue.PQueueJyoutaiDef WillEnqueue;
WillEnqueue.Node = EachEdge.ToNode;
WillEnqueue.SumTime = wkSumTime;
InsPQueue.Enqueue(WillEnqueue);
}
};
EnqueueAct(1);
while (InsPQueue.IsEmpty() == false) {
PQueue.PQueueJyoutaiDef Dequeued = InsPQueue.Dequeue();
//確定ノードならContinue
if (KakuteiNodeDict.ContainsKey(Dequeued.Node)) continue;
KakuteiNodeDict.Add(Dequeued.Node, Dequeued.SumTime);
EnqueueAct(Dequeued.Node);
}
if (KakuteiNodeDict.ContainsKey((int)mN)) {
Console.WriteLine(KakuteiNodeDict[(int)mN]);
}
else {
Console.WriteLine(-1);
}
}
// 現在時間と、枝のCとD、を引数として、最小の移動コストを返す
static long DeriveMinTime(long pC, long pD, long pCurrT)
{
if (WillWait(pC, pD, pCurrT) == false) {
return CalcFunc(pC, pD, pCurrT);
}
long L = pCurrT;
long R = pCurrT + pD * 2;
while (L + 1 < R) {
long Mid = (L + R) / 2;
if (WillWait(pC, pD, Mid)) {
L = Mid;
}
else {
R = Mid;
}
}
return R - pCurrT + CalcFunc(pC, pD, R);
}
// 待った場合に、得な時間があるかを判定
static bool WillWait(long pC, long pD, long pCurrT)
{
long BaseVal = CalcFunc(pC, pD, pCurrT);
for (long I = 1; I < long.MaxValue; I++) {
long wkT = pCurrT + I;
long CurrVal = CalcFunc(pC, pD, wkT);
if (BaseVal > I + CurrVal) return true;
if (BaseVal + 2 <= I + CurrVal) return false;
}
return false;
}
static long CalcFunc(long pC, long pD, long pCurrT)
{
return pC + pD / (pCurrT + 1);
}
}
#region PQueue
// 優先度付きキュー
internal class PQueue
{
internal struct PQueueJyoutaiDef
{
internal long Node;
internal long SumTime;
}
private Dictionary<long, PQueueJyoutaiDef> mHeapDict = new Dictionary<long, PQueueJyoutaiDef>();
internal bool IsEmpty()
{
return mHeapDict.Count == 0;
}
// エンキュー処理
internal void Enqueue(PQueueJyoutaiDef pAddJyoutai)
{
long CurrNode = 1 + mHeapDict.Count;
mHeapDict[CurrNode] = pAddJyoutai;
while (1 < CurrNode && mHeapDict[CurrNode / 2].SumTime > mHeapDict[CurrNode].SumTime) {
PQueueJyoutaiDef Swap = mHeapDict[CurrNode];
mHeapDict[CurrNode] = mHeapDict[CurrNode / 2];
mHeapDict[CurrNode / 2] = Swap;
CurrNode /= 2;
}
}
// デキュー処理
internal PQueueJyoutaiDef Dequeue()
{
PQueueJyoutaiDef TopNode = mHeapDict[1];
long LastNode = mHeapDict.Count;
mHeapDict[1] = mHeapDict[LastNode];
mHeapDict.Remove(LastNode);
MinHeapify(1);
return TopNode;
}
// 根ノードを指定し、根から葉へヒープ構築
private void MinHeapify(long pRootNode)
{
if (mHeapDict.Count <= 1) {
return;
}
long Left = pRootNode * 2;
long Right = pRootNode * 2 + 1;
// 左の子、自分、右の子で値が最小のノードを選ぶ
long Smallest = mHeapDict[pRootNode].SumTime;
long SmallestNode = pRootNode;
if (mHeapDict.ContainsKey(Left) && mHeapDict[Left].SumTime < Smallest) {
Smallest = mHeapDict[Left].SumTime;
SmallestNode = Left;
}
if (mHeapDict.ContainsKey(Right) && mHeapDict[Right].SumTime < Smallest) {
Smallest = mHeapDict[Right].SumTime;
SmallestNode = Right;
}
// 子ノードのほうが大きい場合
if (SmallestNode != pRootNode) {
PQueueJyoutaiDef Swap = mHeapDict[SmallestNode];
mHeapDict[SmallestNode] = mHeapDict[pRootNode];
mHeapDict[pRootNode] = Swap;
// 再帰的に呼び出し
MinHeapify(SmallestNode);
}
}
}
#endregion