This page is still under construction.

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

Error Report

Time limit2sMemory limit512 MB

Summary
Given a sequence of function numbers from concatenated stack traces, build a call graph of minimum edge count where all traces come from errors in at most two functions.
Level

Medium7 of 10

Topics
Graph, Greedy, String, Implementation
Solved
No attempts yet

Problem

A function call stack trace printed when an error occurs is a powerful tool for debugging programs. Consider a mathematical model of how functions in a program interact, called a call graph.

Suppose a program has nn functions that can call one another. Number all functions in the program from 1 to nn. Let EE be the set of pairs (fi,gi)(f_i, g_i), where fif_i and gig_i are the numbers of functions such that fif_i calls gig_i. The size of this set is called the complexity of the call graph.

For example, consider a program in a primitive programming language with three functions.

function f(x)
    if x > 0 then
        return g(x)
    else
        return h(x)
 
function g(x)
    return x
 
function h(x)
    if x == 0 then
        return 1 / x
    else
        return h(x + 1) + 1

Number the functions so that ff is 1, gg is 2, and hh is 3. Then the set EE is E={(1,2),(1,3),(3,3)}E = \{(1, 2), (1, 3), (3, 3)\} because function ff calls functions gg and hh, function gg calls no other functions, and function hh calls itself. The complexity of the call graph of this program is 3.

The call stack trace printed when an error occurs works as follows. Suppose some error occurs while the program is running. First, the number of the function i1i_1 in which the error occurred is printed, then the number of the function i2i_2 from which this function i1i_1 was directly called, then the number of the function i3i_3 from which function i2i_2 was called, and so on.

For example, suppose the call f(−3)f(-3) is made in the program above. Then h(−3)h(-3) is called, which calls h(−2)h(-2), which in turn calls h(−1)h(-1) and h(0)h(0), and division by 0 occurs in the last call. Then the call stack trace looks like this:

3
3
3
3
1

Yura asked Lyosha to find an error in his program and sent him the call stack traces printed after errors. Unfortunately, the file Yura sent to Lyosha contains several call stack traces from different errors, and these traces are printed one after another with no separators between them. Yura claims that errors can occur in only two functions, but he does not remember which ones.

Lyosha realized that the call stack traces alone would not be enough and that he would have to look at the program. Before doing so, he wants to find out what minimum complexity the call graph of this program can have if all of Yura's claims are true.

Help him find the minimum possible complexity of the program's call graph such that the file sent to him could contain one or more call stack traces written one after another without separators, and the direct errors occurred in at most two different functions.

Input

The first line contains two integers nn and mm (1≤n,m≤10001 \le n, m \le 1000): the number of functions in Yura's program and the number of lines in the call stack trace file that Yura sent to Lyosha. Each of the following mm lines contains a single integer fif_i (1≤fi≤n1 \le f_i \le n): the number of the function on the ii-th line of the file.

Output

On the first line, print a single integer kk: the minimum possible complexity of the call graph of Yura's program. On each of the following kk lines, print two integers aia_i and bib_i. Such a pair means that the function numbered aia_i can call the function numbered bib_i. If there are several possible call graphs, print any one of them.

Hint

In the example, it is possible that errors occur only in functions 1 and 3, and that the given file contains five call stack traces written one after another. Below are the same traces separated by empty lines.

1
 
3
 
3
2
 
3
2
 
1

In this case only function 2 calls function 3, so the complexity of the call graph is 1.

Examples1

  1. Example 1

    Input
    3 7
    1
    3
    3
    2
    3
    2
    1
    
    Expected output
    1
    2 3