@SuppressWarnings("unchecked")
  public void solve() throws IOException {
    int n, k;
    while ((n = nextInt()) != 0 && (k = nextInt()) != 0) {
      children = new LinkedList[n];
      worths = new int[n];

      for (int x = 0; x < n; x++) {
        children[x] = new LinkedList<Integer>();
      }

      int root = -1;
      for (int x = 0; x < n; x++) {
        int p = nextInt();
        int w = nextInt();
        if (p == 0) {
          root = x;
        } else {
          children[p - 1].add(x);
        }
        worths[x] = w;
      }

      int[] max = new int[k];
      max = recurse(root, max);

      System.out.println(max[k - 1]);
    }
  }