#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 500;
int n, m, fa[N], sz[N], son[N], dep[N], pos[N], id[N],
    seed = 0, top[N], T[N << 2], ans[N], tag[N << 2];
vector<int> to[N];
struct node {
  int x, y, id;
} q[N];
bool cmp(node a, node b) {
  int ta = a.y + dep[a.x];
  int tb = b.y + dep[b.x];
  return ta == tb ? a.x < b.x : ta < tb;
}
void dfs(int t) {
  sz[t] = 1;
  son[t] = 0;
  for (int i = 0; i <= (int)to[t].size() - 1; i++) {
    dep[to[t][i]] = dep[t] + 1;
    dfs(to[t][i]);
    sz[t] += sz[to[t][i]];
    if (!son[t] || sz[to[t][i]] > sz[son[t]]) son[t] = to[t][i];
  }
}
void Dfs(int t, int tp) {
  pos[t] = ++seed;
  id[seed] = t;
  top[t] = tp;
  if (son[t]) Dfs(son[t], tp);
  for (int i = 0; i <= (int)to[t].size() - 1; i++)
    if (to[t][i] != son[t]) Dfs(to[t][i], to[t][i]);
}
void build(int x, int l, int r) {
  if (l == r) {
    T[x] = l == pos[0] ? 0x7fff7fff : 0;
    return;
  }
  int mi = (l + r) >> 1;
  build(x << 1, l, mi);
  build(x << 1 | 1, mi + 1, r);
  T[x] = max(T[x << 1], T[x << 1 | 1]);
}
void pd(int x, int l, int r) {
  if (tag[x] != 0) {
    int mi = (l + r) >> 1;
    T[x << 1] = tag[x] + mi - l + dep[id[mi]];
    T[x << 1 | 1] = T[x];
    tag[x << 1] = tag[x];
    tag[x << 1 | 1] = tag[x] + (mi + 1 - l);
    tag[x] = 0;
  }
}
void modify(int x, int l, int r, int L, int R, int st) {
  if (L <= l && r <= R) {
    T[x] = st + (r - L) + dep[id[r]];
    tag[x] = st + (l - L);
    return;
  }
  int mi = (l + r) >> 1;
  pd(x, l, r);
  if (L <= mi) modify(x << 1, l, mi, L, R, st);
  if (mi < R) modify(x << 1 | 1, mi + 1, r, L, R, st);
  T[x] = max(T[x << 1], T[x << 1 | 1]);
}
int ask(int x, int l, int r, int L, int R, int lim) {
  if (T[x] <= lim) return -1;
  if (l == r) return id[l];
  int mi = (l + r) >> 1, ans = -1;
  pd(x, l, r);
  if (mi < R) ans = ask(x << 1 | 1, mi + 1, r, L, R, lim);
  if (ans == -1 && L <= mi) ans = ask(x << 1, l, mi, L, R, lim);
  return ans;
}
void update(int x, int y, int st) {
  while (top[x] ^ top[y]) {
    modify(1, 1, n + 1, pos[top[y]], pos[y], st + dep[top[y]] - dep[x]);
    y = fa[top[y]];
  }
  modify(1, 1, n + 1, pos[x], pos[y], st);
}
int ask(int x, int y) {
  for (int l = -1;; l = top[x], x = fa[top[x]]) {
    int t = ask(1, 1, n + 1, pos[top[x]], pos[x], y);
    if (t != -1) {
      if (t == x)
        return l;
      else
        return son[t];
    }
  }
}
int solve(int x, int y) {
  int t = ask(x, dep[x] + y);
  if (t == -1) return y;
  update(t, x, y + dep[x] - dep[t] + 2);
  return 2 * (dep[x] - dep[t] + 1) + y;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", fa + i);
    to[fa[i]].push_back(i);
  }
  dfs(0);
  Dfs(0, 0);
  build(1, 1, n + 1);
  for (int i = 1; i <= m; i++) scanf("%d%d", &q[i].x, &q[i].y), q[i].id = i;
  sort(q + 1, q + m + 1, cmp);
  for (int i = 1; i <= m; i++) ans[q[i].id] = solve(q[i].x, q[i].y);
  for (int i = 1; i <= m; i++) printf("%d ", ans[i]);
  puts("");
  return 0;
}
