Polish version    English version  
  About Olympic -> Problems


 News
 About Olympic
About contest
Problems
I OI 1993/1994
II OI 1994/1995
III OI 1995/1996
IV OI 1996/1997
V OI 1997/1998
VI OI 1998/1999
VII OI 1999/2000
VIII OI 2000/2001
IX OI 2001/2002
X OI 2002/2003
XI OI 2003/2004
XII OI 2004/2005
XIII OI 2005/2006
XIV OI 2006/2007
XV OI 2007/2008
Problems archive
 History of OI
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
VI Olimpiada Informatyczna 1998/1999

Task: PIE
Author:
Primitivus

III stage contest  

A genetic code of the abstract primitivus (Primitivus recurencis) is a series of natural numbers K=(a_1,...,a_n). A feature of primitivus we call each ordered pair of numbers (l,r), which appears successively in the genetic code, i.e. there exists such i that l=a_i, r=a_i+1. There are no (p,p) features in a primitivus' genetic code. 

Task

Write a program which: 

  • reads the list of the features from the text file PIE.IN,
  • computes the length of the shortest genetic code having given features,
  • writes the results in the output file PIE.OUT. 

Input

In the first line of the input file PIE.IN one positive integer number n is written. It is the number of different features of the primitivus. In each of the following n lines there is a pair of natural numbers l and r separated by a single space, 1 <= l <= 1000, 1 <= r <= 1000. A pair (l, r) is one of the primitivus' features. The features do not repeat in the input file

Output

Your program should write, in the first and only line of the text file PIE.OUT, exactly one integer number equal to the length of the shortest genetic code of the primitivus, comprising the features from the file PIE.IN. 

Example

For the input file PIE.IN

12
2 3
3 9
9 6
8 5
5 7
7 6
4 5
5 1
1 4
4 2
2 8
8 6
the correct answer is the file PIE.OUT 


15

All the features from the file PIE.IN are written in the following genetic code: 

(8, 5, 1, 4, 2, 3, 9, 6, 4, 5, 7, 6, 2, 8, 6)




Print friendly version