The White Rabbit's Pocket Watch

Given walks whose total length is known only modulo 13, recover each edge's true weight in 1..12 and find the shortest path from A to R.

Medium7GraphNumber theoryShortest pathMathNo attempts yetTime limit2sMemory limit512 MB

Problem

The White Rabbit's pocket watch shows only the hours 0 to 12, and past 12 it returns to 0. When a trip ends, the Rabbit writes down whatever number the pointer rests on. A trip that really took 14 hours leaves the pointer on 1, so his logbook records 1. Every duration in the logbook is therefore the real duration taken modulo 13.

Wonderland has NN locations. There is at most one track directly joining two locations, and a track takes the same real time in both directions. The real time of one track is an integer between 1 and 12.

The logbook holds TT trips. One entry has the time read off the watch, the number of locations passed, and the order in which they were passed. The same location can appear several times. Two locations that are neighbours in the order are joined by a direct track, and the tracks that appear in the logbook are all the tracks of Wonderland.

Alice is at location AA and wants to reach the Rabbit's hole at location RR. Find the smallest real time of a route from AA to RR.

The logbook alone fixes the real time of every track, and at least one route from AA to RR exists.

Input

The first line contains the integers NN, AA, RR and TT separated by spaces. NN is the number of distinct locations, AA is the location where Alice is, RR is the location of the Rabbit's hole, and TT is the number of trips written in the logbook. Locations are numbered from 1 to NN.

Each of the next TT lines describes one trip in the format d p a1 a2 ... ap, where dd is the trip time the Rabbit read off the watch, pp is the number of locations passed, and a1 a2  apa_1\ a_2\ \dots\ a_p is the order in which they were passed.

Output

Print one integer, the shortest real time it takes Alice to reach the Rabbit's hole.

Constraints

  • 2N2002 \le N \le 200
  • 1A,RN1 \le A, R \le N
  • 1T5001 \le T \le 500
  • 0d120 \le d \le 12
  • 2p8002 \le p \le 800
  • 1aiN1 \le a_i \le N
  • There are at most 200 distinct tracks.
  • The real time dijd_{ij} of one track is an integer with 1dij121 \le d_{ij} \le 12.