import
java.util.ArrayList;
import
java.util.Arrays;
import
java.util.PriorityQueue;
class
GFG{
static
class
Pair
implements
Comparable<Pair>
{
int
first, second;
public
Pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
@Override
public
int
compareTo(GFG.Pair o)
{
if
(
this
.first == o.first)
{
return
this
.second - o.second;
}
return
this
.first - o.first;
}
}
static
final
int
MAXX =
10000
, INF = (
int
)1e9;
@SuppressWarnings
(
"unchecked"
)
static
ArrayList<Pair>[] adj =
new
ArrayList[MAXX *
10
+
3
];
static
int
[] dist =
new
int
[MAXX *
10
+
3
];
static
int
even(
int
x)
{
return
x *
10
+
2
;
}
static
int
odd(
int
x)
{
return
x *
10
+
1
;
}
static
void
addEdge(
int
a,
int
b,
int
cost)
{
adj[even(a)].add(
new
Pair(odd(b), cost));
adj[odd(a)].add(
new
Pair(even(b), cost));
adj[odd(b)].add(
new
Pair(even(a), cost));
adj[even(b)].add(
new
Pair(odd(a), cost));
}
static
int
dijkstra(
int
source,
int
destination)
{
PriorityQueue<Pair> pq =
new
PriorityQueue<>();
pq.add(
new
Pair(
0
, even(source)));
dist[even(source)] =
0
;
while
(!pq.isEmpty())
{
int
u = pq.poll().second;
for
(Pair p : adj[u])
{
int
v = p.first;
int
c = p.second;
if
(dist[u] + c < dist[v])
{
dist[v] = dist[u] + c;
pq.add(
new
Pair(dist[v], v));
}
}
}
return
dist[even(destination)];
}
public
static
void
main(String[] args)
{
for
(
int
i =
0
; i < MAXX *
10
+
3
; i++)
{
adj[i] =
new
ArrayList<Pair>();
}
Arrays.fill(dist, INF);
int
n =
5
, m =
6
;
addEdge(
1
,
2
,
1
);
addEdge(
2
,
3
,
2
);
addEdge(
2
,
5
,
15
);
addEdge(
3
,
5
,
1
);
addEdge(
3
,
4
,
4
);
addEdge(
5
,
4
,
3
);
int
source =
1
;
int
destination = n;
int
ans = dijkstra(source, destination);
if
(ans == INF)
System.out.println(
"-1"
);
else
System.out.println(ans);
}
}