Consider a machine with
integer registers
and a single type of
compare-exchange instruction, CE
defined as follows, where
are the register indices:
CE: if content(
)
content(
) then
exchange the contents of registersand
.
A compare-exchange program (shortly CE-program)
is any finite sequence of compare-exchange instructions.
A CE-program is called minimum-finding if
after its execution the register
always contains the minimum value
among all the initial values in the registers.
Furthermore, such program is called reliable if it
remains a minimum-finding program after removing any single
compare-exchange instruction.
Given a CE-program
, what is the minimum number of
instructions that should be added at the end of program
in order to make it reliable?
Example
Consider the following 3-register CE-program: CE
; CE
; CE
.
In order to make this program reliable it suffices to
add only two extra instructions, namely CE
and CE
.
Problem
Write a program that reads the description of a CE-program,
and determines the minimum number of CE-instructions that
should be added to make this program reliable.
Input specification
The first line of input contains the number of registers
,
where
,
followed by the number of program instructions
,
where
.
The next line contains the program itself: a sequence of
integers,
separated by spaces, where each CE-instruction consists of
two consecutive integers
on positions
and
, with
.
Output specification
The output consists of a single integer: the minimal number of instructions
that should be added to the input program in order to make this
program reliable.
Sample input
3 3 1 2 2 3 1 2
2