1492. History

"The monster from Corsica landed at the port of Juan."

"The unspeakable Cannibal is approaching Graz."

"The despicable usurper entered Gellernoble."

"Napoleon Bonaparte takes Lyon."

"General Napoleon approaches Fontainebleau."

"The supreme emperor arrived in his faithful Paris today!"

One day when you wake up,you find that you have become Napoleon! Now,you will lead the army to capture Paris and rebuild your own dynasty!

In France, there are nn cities and mm directed roads. Each road is defined as , means that there is a road from xix_i to yiy_i, which takes ziz_i days. It is guaranteed that there is no self loop.

However,the battlefield is changing rapidly,so the time required to move on the road is constantly changing. Specifically,every time after you move on one road,the time spent on all roads changes from ziz_i to f(zi)f(z_i).

f(x)=1+x1x   mod p          x(1,p1) f(x)=\frac{1+x}{1-x} \ \ \ mod \ p \ \ \ \ \ \ \ \ \ \ x\in(1,p - 1)

It is guaranteed that pp is a prime number and f(zi)f(z_i) is defined in any time.

Now,please try to use she shortest time to arrived in the city nn from the city 11.

输入

The first line contains three integers n,m,pn,m,p (1n,m2105,5p109)(1 \leq n,m \leq 2*10^5, 5\leq p \leq 10^9).

The next mm lines describe the roads. Each line contains three integer xi,yi,zi(1xi,yin,1<zi<p1)x_i,y_i,z_i(1\leq x_i , y_i\leq n,1 < z_i < p-1) , denoting that there is a road from city xix_i to yiy_i, which takes ziz_i days.

输出

One integer, denoting the shortest time.

样例

标准输入 复制文本
4 5 5
1 2 2
3 4 2
1 3 2
2 4 2
2 3 3
标准输出 复制文本
4

来源

2021 GDCPC 广东省大学生程序设计竞赛

登录以提交代码。
单点时限 2 秒
内存限制 512 MB
提交 7
通过 2