Polish version    English version  
  History of OI -> VII OI 1999/2000 -> Problems


 News
 About Olympic
 History of OI
XVII OI 2009/2010
XVI OI 2008/2009
XV OI 2007/2008
XIV OI 2006/2007
XIII OI 2005/2006
XII OI 2004/2005
XI OI 2003/2004
X OI 2002/2003
IX OI 2001/2002
VIII OI 2000/2001
VII OI 1999/2000
Stage III - results
Stage II - results
Stage I - results
Problems
Regulations
Organization
Hints
Schedule
Stats
VI OI 1998/1999
V OI 1997/1998
IV OI 1996/1997
III OI 1995/1996
II OI 1994/1995
I OI 1993/1994
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
VII Olimpiada Informatyczna 1999/2000

Task: POL
Author: Grzegorz Jakacki
P-broken-line

III stage contest  

In the rectangular coordinate system every point with integer coordinates is called p-point. Any segment with different ends being p-points, parallel to one of the axis of coordinates, is called p-segment. Only closed segments (with end points belonging to the segment) are taken into consideration. A broken line built from k p-segments, in which every two consecutive ones are perpendicular, we call p-broken-line of degree k.

Task

Write a program, which:

  • reads descriptions of a certain number of p-segments and coordinates of two different p-points A and B, from the text file POL.IN,
  • determines minimum degree of a p-broken-line connecting these two points and not crossing any from given p-segments or states, that such a p-broken-line does not exist.
  • writes the result to the text file POL.OUT.

Input

Description of a p-point consists of two non-negative integers, being being coordinates x and y adequately of this p-point, separated by a single space, These numbers are from range [0..1000000000]. The first line of the input file POL.IN contains only the description of the p-point A. The second line contains only the description of the p-point B. The third line consists exactly of one non-negative integer n being the number of p-segments, 1 <= n <= 50. Each of next n lines consists of descriptions of exactly two p-points, separated by a single space. These are coordinates of the ends of one p-segment.

Output

The first and the the only line of text file POL.OUT should contain either one number being minimum degree of p-broken-line connecting points A and B and not crossing any of given p-segments, or the word BRAK, if a p-broken-line having above-mentioned properties does not exist.

Example

For the input file POL.IN:

1 2
3 4
5
0 0 7 0
0 5 7 5
2 2 2 7
4 0 4 3
3 2 6 2

the correct result is the output file POL.OUT:

5



Print friendly version