This page is still under construction.

Parts of this page are still being built. What you see may change.

Mixed Flight Plans

Time limit1sMemory limit128 MB

Summary
Compute the cheapest cost to send two bags with direct and fixed-price indirect flights, either separately or by swapping bags at a shared airport.
Level

Hard8 of 10

Topics
Shortest path, Graph, Brute force
Solved
No attempts yet

Problem

Ad Hoc Postal Company (AHPC) delivers postal envelopes in a simple way. It advertises for a volunteer, buys that volunteer the cheapest flight plan from the source city to the destination city, and hands over a bag of envelopes at the source airport. The volunteer gives the bag to the company's correspondent at the destination airport.

Besides direct flights between two airports, there are indirect trips that stop at several airports in a fixed order. A passenger who buys an indirect trip has to board at its first airport and passes the airports one after another in the listed order, with no other direct flight or indirect trip in between. The passenger may give up the rest of the trip and get off at any intermediate airport. The price of an indirect trip is fixed, so it costs the same whether the passenger takes every flight of it or stops in the middle. A flight plan is built from direct flights and indirect trips (whole or partial), and its price is the sum of the prices of everything bought for it.

Two bags have to be sent, one from airport AA to BB and the other from CC to DD, where AA, BB, CC, DD are four different airports. The company picks one of these two arrangements.

  • Buy a plan from AA to BB for the first volunteer and a plan from CC to DD for the second one.
  • Mix the two plans to save money. Buy a plan from AA to DD for the first volunteer and a plan from CC to BB for the second one. If both plans stop at the same airport MM, the two volunteers meet at MM and exchange their bags, so the first one carries the second bag to DD and the second one carries the first bag to BB.

MM may be one of AA, BB, CC, DD, and it may also be an airport that a volunteer only passes while riding an indirect trip, without getting off there. The first volunteer has to pass MM before arriving at DD, and the second one has to pass MM before arriving at BB. The total price is the price of the first plan plus the price of the second plan.

The figure above shows six airports with direct flights (solid arrows), indirect trips (dashed and dotted arrows), and their prices. One bag goes from airport 3 (AA) to 5 (BB), the other from 6 (CC) to 1 (DD). If the company buys (3,4) and (4,5) for the first volunteer and (6,5) and (5,1) for the second one, the total price is 300. A cheaper choice is (3,4,1,2,6) for the first volunteer and (6,2) with (2,4,5) for the second one, for a total of 250. The volunteers meet at airport 4 (MM) and swap bags there, and the first volunteer leaves the indirect trip at airport 1.

Given the flight information, write a program that finds the cheapest cost of delivering the two bags.

Input

The input has several test cases. The first line of each case has six integers nn, mm, AA, BB, CC, DD. nn (4≤n≤1004 \le n \le 100) is the number of airports, numbered 1 through nn. mm (0≤m≤10 0000 \le m \le 10\,000) is the number of direct flights and indirect trips together.

The iith of the next mm lines starts with two positive integers pip_i and sis_i. pip_i (pi≤106p_i \le 10^6) is the price, and sis_i is 1 for a direct flight and 2 or more for an indirect trip. Then come si+1s_i + 1 distinct airport numbers in the order they are visited.

The line after the last case is 0 0 0 0 0 0, which must not be processed.

Output

For each test case, print the cheapest delivery cost on one line. If neither arrangement above can deliver both bags, print Impossible! without the quotes.

Examples1

  1. Example 1

    Input
    6 9 3 5 6 1
    100 1 3 4
    50 1 6 2
    100 2 2 4 5
    50 1 6 5
    100 1 1 3
    100 4 3 4 1 2 6
    100 1 5 1
    50 1 4 5
    50 1 2 3
    4 0 1 2 3 4
    5 2 1 2 3 4
    10 4 1 2 5 3 4
    20 1 3 5
    0 0 0 0 0 0
    
    Expected output
    250
    Impossible!
    Impossible!