#include <bits/stdc++.h>
using namespace std;
const int N = 200005, inf = 2000000000;
int a[N], t[N], id[N], fa[N], dep[N], num[N], son[N], q[N], dfn[N], top[N],
    mx[N << 2], tag[N << 2], ans[N], n, m, cnt = 0, fd;
bool cmp(int x, int y) {
  if (t[x] + dep[a[x]] != t[y] + dep[a[y]])
    return t[x] + dep[a[x]] < t[y] + dep[a[y]];
  return a[x] < a[y];
}
void pushup(int p) { mx[p] = max(mx[p << 1], mx[p << 1 ^ 1]); }
void pushdown(int p, int l, int mid, int r) {
  if (tag[p] < -inf) return;
  tag[p << 1] = tag[p << 1 ^ 1] = tag[p];
  mx[p << 1] = mid * 2 + tag[p];
  mx[p << 1 ^ 1] = r * 2 + tag[p];
  tag[p] = -inf - 1;
}
void modify(int p, int l, int r, int x, int y, int z) {
  if (x <= l && r <= y) {
    tag[p] = z;
    mx[p] = r * 2 + z;
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(p, l, mid, r);
  if (x <= mid) modify(p << 1, l, mid, x, y, z);
  if (y > mid) modify(p << 1 ^ 1, mid + 1, r, x, y, z);
  pushup(p);
}
void query(int p, int l, int r, int x, int y, int z) {
  if (mx[p] <= z) return;
  if (l == r) {
    fd = q[l];
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(p, l, mid, r);
  if (fd == -1 && y > mid) query(p << 1 ^ 1, mid + 1, r, x, y, z);
  if (fd == -1 && x <= mid) query(p << 1, l, mid, x, y, z);
  pushup(p);
}
int work(int x, int t) {
  int y = x;
  for (; 1; y = fa[top[y]]) {
    fd = -1;
    query(1, 0, n, dfn[top[y]], dfn[y], t + dfn[y]);
    if (fd >= 0) {
      t += dep[y] - dep[fd];
      y = fd;
      break;
    }
    if (!y) break;
    t += dep[y] - dep[fa[top[y]]];
  }
  int z = t + dep[x] - dep[y];
  for (; 1; x = fa[top[x]]) {
    if (top[x] == top[y]) {
      if (x != y) modify(1, 0, n, dfn[y] + 1, dfn[x], t - dfn[y]);
      break;
    }
    modify(1, 0, n, dfn[top[x]], dfn[x], dep[x] - dep[y] + t - dfn[x]);
  }
  return z;
}
int main() {
  scanf("%d%d\n", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &fa[i]);
    dep[i] = dep[fa[i]] + 1;
    num[i] = 1;
  }
  for (int i = n; i; i--) {
    if (num[i] > num[son[fa[i]]]) son[fa[i]] = i;
    if (fa[i]) num[fa[i]] += num[i];
  }
  for (int i = son[0]; i; i = son[i]) q[dfn[i] = ++cnt] = i;
  for (int i = 0; i <= n; i++)
    if (!dfn[i])
      for (int j = i; j; j = son[j]) top[q[dfn[j] = ++cnt] = j] = i;
  for (int i = 1; i <= m; i++) scanf("%d%d\n", &a[i], &t[i]);
  for (int i = 1; i <= m; i++) id[i] = i;
  sort(id + 1, id + m + 1, cmp);
  mx[1] = tag[1] = -inf;
  for (int i = 1; i <= m; i++) ans[id[i]] = work(a[id[i]], t[id[i]]);
  for (int i = 1; i <= m; i++) printf("%d ", ans[i]);
  return 0;
}
