Problem:
Problem Statement

We have N boxes, numbered 1 through N. At first, box 1 contains one red ball, and each of the other boxes contains one white ball.

Snuke will perform the following M operations, one by one. In the i-th operation, he randomly picks one ball from box x_i, then he puts it into box y_i.

Find the number of boxes that may contain the red ball after all operations are performed.

Constraints

2≤N≤10^5

1≤M≤10^5

1≤x_i,y_i≤N

x_i≠y_i

Just before the i-th operation is performed, box x_i contains at least 1 ball.

Input

The input is given from Standard Input in the following format:

N M
x_1 y_1
:
x_M y_M

Output

Print the number of boxes that may contain the red ball after all operations are performed.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

2

Just after the first operation, box 1 is empty, box 2 contains one red ball and one white ball, and box 3 contains one white ball.

Now, consider the second operation. If Snuke picks the red ball from box 2, the red ball will go into box 3. If he picks the white ball instead, the red ball will stay in box 2.
Thus, the number of boxes that may contain the red ball after all operations, is 2.

Sample Input 2

3 3
1 2
2 3
2 3

Sample Output 2

1

All balls will go into box 3.

Sample Input 3

4 4
1 2
2 3
4 1
3 4

Sample Output 3

3

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.*;
import java.util.*;
public class Main {
  static int r_box[];
  static int w_box[];
  static int nn = 0;
  static int mm = 0;
  static int[][] moveData;
  static  Set<Integer> r_pos;

  public static void main(String[] args) throws Exception {
    String line = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    //1行目のデータの読み込み
    line = br.readLine();
    String input[] = line.split(" ");
    nn = Integer.parseInt(input[0]); // 箱の数
    mm = Integer.parseInt(input[1]); // 移動データの数
    //System.out.println("箱の数:" + nn + ",操作の数:" + mm);
    r_box = new int[nn];
    w_box = new int[nn];

    //2行目以降(移動データ)の読み込み
    moveData = new int[mm][2];
    for (int i = 0; i < mm; i++) {
      line = br.readLine();
      input = line.split(" ");
      int xx = Integer.parseInt(input[0]); // 移動前
      int yy = Integer.parseInt(input[1]); // 移動後
      moveData[i][0] = xx;
      moveData[i][1] = yy;
    }

    // すべての組み合わせをリスト
    int[][] intArray = new int[mm][2];
    for (int i = 0; i < mm; i++) {
      for (int j = 0; j < 2; j++) {
        intArray[i][j] = j;
      }
    }
    r_pos = new HashSet<Integer>();
    getAllPattern(0, intArray, "");
    System.out.println(r_pos.size());
  }

  // それぞれの玉の位置の初期化
  public static void initBox() {
    for (int i = 0; i < nn; i++) {
      if (i == 0) {
        r_box[i] = 1;
        w_box[i] = 0;
      } else {
        r_box[i] = 0;
        w_box[i] = 1;
      }
    }
  }

  // 赤玉と白玉の位置を出力
  public static void printBox() {
    for (int i = 0; i < nn; i++) {
      System.out.println((i + 1) + ":R=" + r_box[i] + ",W=" + w_box[i]);
    }
  }

  // 赤玉を移動
  public static void r_move(int a, int b) {
    r_box[(a - 1)] = r_box[(a - 1)] - 1;
    r_box[(b - 1)] = r_box[(b - 1)] + 1;
  }

  // 白玉を移動
  public static void w_move(int a, int b) {
    w_box[(a - 1)] = w_box[(a - 1)] - 1;
    w_box[(b - 1)] = w_box[(b - 1)] + 1;
  }

  // 赤/白選択のすべての組み合わせを作成
  static void getAllPattern(int i, int[][] intArray, String output){
    if (i == intArray.length) {
      doMove(output);
      return;
    }
    String del = (output.length() > 0) ? "," : "";
    for (int j = 0; j < intArray[i].length; j++) {
      getAllPattern(i + 1, intArray, output + del + intArray[i][j]);
    }
  }

  static void doMove(String rw_Data) {
    // 箱の初期化
    initBox();
    //System.out.println("START: -- " + rw_Data + "-----");
    String val[] = rw_Data.split(",");
    // 赤玉または白玉の移動
    int moved = 0;
    //printBox(); // 移動前の位置
    for (int i = 0; i < mm; i++) {
      int xx = moveData[i][0]; // 移動前
      int yy = moveData[i][1]; // 移動後
      int rr = Integer.parseInt(val[i]);
      if (rr == 1) {
        if (r_box[(xx -1)] == 0) {
          //System.out.println("赤玉がない");
          return;
        } else {
          r_move(xx, yy);
          moved = yy;
          //System.out.println("赤玉を移動:" + xx + " --> " + yy);
        }
      } else {
        if (w_box[(xx -1)] == 0) {
          //System.out.println("白玉がない");
          return;
        } else {
          w_move(xx, yy);
          //System.out.println("白玉を移動:" + xx + " --> " + yy);
        }
      }
      //printBox(); // 移動前の位置
    }
    r_pos.add(moved);
    //System.out.println("最後の位置:" + moved);
    return;
  }

}