As you may already know, different countries in Europe use different railroad systems. Not only do they use different voltages for their trains, but also the distance between the two rails (gauge) differs. The following table shows some railway gauges used:
Broad gauge (Spain): | 1674 mm |
---|
Broad gauge (Portugal): | 1665 mm |
---|
Broad gauge (Ireland): | 1600 mm |
---|
Broad gauge (Finland): | 1524 mm |
---|
Broad gauge (former USSR): | 1520 mm |
---|
Standard gauge: | 1435 mm |
---|
Narrow gauge (meter gauge): | 1000 mm |
---|
A museum has trains from several countries. It needs tracks for every train type in order to show visitors the trains in use. However, since only one train is used at a time, a rail can be used by trains of different types. It follows that for n trains, each requiring a different railway gauge, n + 1 rails are sufficient (each train uses the leftmost rail and a rail that has exactly the required distance to it). But sometimes it is possible to save even more rails.
Given the required railway gauges, your task is to construct a railway track that can be used by every train and requires the least number of rails. Note that a train can use any two rails, provided the distance between them is right.