import java.util.*; public class Exercise51{ public static void main (String[] args){ Scanner sc = new Scanner(System.in); int vert = sc.nextInt(); int hori = sc.nextInt(); int[][] lines = new int[hori][2]; for (int a = 0; a < hori; a++){ lines[a][0] = sc.nextInt(); lines[a][1] = sc.nextInt(); } int[] answer = new int[vert]; for (int b = 0; b < vert; b++){ answer[b] = sc.nextInt(); } ArrayList newLines = new ArrayList(); for (int i = 0; i < vert; i++){ int currentNum = i + 1; for (int j = 0; j < hori; j++){ if (currentNum == lines[j][0]){ currentNum++; }else if(currentNum == lines[j][1]){ currentNum--; } } for (int k = 0; k < newLines.size(); k++){ int[] newLine = new int[2]; newLine = newLines.get(k); if (currentNum == newLine[0]){ currentNum++; }else if(currentNum == newLine[1]){ currentNum--; } } // public static int[] addNewLine(i, answer, currentNum, newLines) addNewLine(i, answer, currentNum, newLines); } System.out.println(newLines.size()); for (int c = 0; c < newLines.size(); c++){ int[] newLine = new int[2]; newLine = newLines.get(c); System.out.println(newLine[0] + " " + newLine[1]); } } public static void addNewLine(int i, int[] answer, int currentNum, ArrayList newLines){ if (answer[i] == currentNum){ return; } else if (answer[i] > currentNum){ int[] newLine = new int[2]; newLine[0] = currentNum; newLine[1] = currentNum + 1; currentNum++; newLines.add(newLine); addNewLine(i, answer, currentNum, newLines); } else if (answer[i] < currentNum){ int[] newLine = new int[2]; newLine[0] = currentNum - 1; newLine[1] = currentNum; currentNum--; newLines.add(newLine); addNewLine(i, answer, currentNum, newLines); } } }