#include <bits/stdc++.h>
using namespace std;
int n, q;
vector<int> e[100010];
int L[100010], R[100010], tot, dis[100010], par[100010][23];
void dfs(int rt, int len) {
  L[rt] = (++tot);
  int Size = e[rt].size();
  dis[rt] = len;
  for (int i = 0; i < (Size); i++) {
    par[e[rt][i]][0] = rt;
    dfs(e[rt][i], len + 1);
  }
  R[rt] = tot;
}
void init() {
  memset(par, -1, sizeof(par));
  dis[1] = 0;
  dfs(1, 0);
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i < (n + 1); i++)
      if (par[i][j - 1] != -1) par[i][j] = par[par[i][j - 1]][j - 1];
}
int LCA(int a, int b) {
  if (dis[a] < dis[b]) swap(a, b);
  int k = 0;
  while ((1 << (k + 1)) <= dis[a]) k++;
  for (int j = k; j > (-1); j--)
    if (dis[a] - (1 << j) >= dis[b]) a = par[a][j];
  for (int i = k; i > (-1); i--)
    if (par[a][i] != -1 && par[a][i] != par[b][i]) a = par[a][i], b = par[b][i];
  if (a == b) return a;
  return par[a][0];
}
int main() {
  scanf("%d %d", &n, &q);
  for (int i = 2; i < (n + 1); i++) {
    int p;
    scanf("%d", &p);
    e[p].push_back(i);
  }
  init();
  while (q--) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    int ab = LCA(a, b);
    int ac = LCA(a, c);
    int bc = LCA(b, c);
    int ch = ab, maxx = dis[ab];
    if (maxx < dis[ac]) {
      ch = ac;
      maxx = dis[ac];
    }
    if (maxx < dis[bc]) {
      ch = bc;
      maxx = dis[bc];
    }
    int d1 = dis[a] + dis[ch] - 2 * dis[LCA(ch, a)];
    int d2 = dis[b] + dis[ch] - 2 * dis[LCA(ch, b)];
    int d3 = dis[c] + dis[ch] - 2 * dis[LCA(ch, c)];
    int ans = max(max(d1, d2), d3);
    printf("%d\n", ans + 1);
  }
}
