#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100001];
int par[100001][31];
int hei[100001];
int n, q;
void dfs(int cur, int p, int h) {
  hei[cur] = h;
  for (int next : adj[cur]) {
    if (next != p) {
      par[next][0] = cur;
      dfs(next, cur, h + 1);
    }
  }
}
void processLca() {
  for (int i = 1; i < 31; i++) {
    for (int j = 1; j <= n; j++) {
      par[j][i] = par[par[j][i - 1]][i - 1];
    }
  }
}
int getLca(int x, int y) {
  if (hei[x] > hei[y]) swap(x, y);
  int d = hei[y] - hei[x];
  for (int i = 30; i >= 0; i--) {
    if (d & (1 << i)) y = par[y][i];
  }
  if (x == y) return x;
  for (int i = 30; i >= 0; i--) {
    if (par[x][i] != par[y][i]) {
      x = par[x][i];
      y = par[y][i];
    }
  }
  return par[x][0];
}
int getdist(int x, int y) {
  int lca = getLca(x, y);
  return hei[x] + hei[y] - 2 * hei[lca];
}
int cntpath(int f, int s, int t) {
  int lc1 = getLca(f, s);
  int lc2 = getLca(f, t);
  int k = 0;
  if (lc1 == lc2) {
    int lc3 = getLca(s, t);
    k = getdist(lc3, lc1);
  }
  return k + min(getdist(lc1, f), getdist(lc2, f)) + 1;
}
int main(void) {
  scanf("%d %d", &n, &q);
  for (int i = 2; i <= n; i++) {
    int t;
    scanf("%d", &t);
    adj[i].push_back(t);
    adj[t].push_back(i);
  }
  dfs(1, -1, 0);
  processLca();
  for (int i = 0; i < q; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    printf("%d\n",
           max(cntpath(a, b, c), max(cntpath(b, a, c), cntpath(c, a, b))));
  }
  return 0;
}
