#include <bits/stdc++.h>
using namespace std;
const int MaxN = 4e5 + 17;
const long long INF = 2e9 + 17;
const int MOD = 1e9 + 7;
const double eps = 1e-3;
const double pi = 3.14159265359;
int n, l;
vector<int> g[MaxN];
int tin[MaxN], tout[MaxN];
int lvl[MaxN];
int timer;
vector<int> up[MaxN];
void dfs(int v, int p = 0, int lv = 0) {
  tin[v] = ++timer;
  up[v][0] = p;
  lvl[v] = lv;
  for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (to != p) dfs(to, v, lv + 1);
  }
  tout[v] = ++timer;
}
bool upper(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; }
int lca(int a, int b) {
  if (upper(a, b)) return a;
  if (upper(b, a)) return b;
  for (int i = l; i >= 0; --i)
    if (!upper(up[a][i], b)) a = up[a][i];
  return up[a][0];
}
int Ans;
void check(int a, int b, int c) {
  int lab = lca(a, b);
  if (lca(lab, c) == c) {
    Ans = max(Ans, abs(lvl[c] - lvl[lab]));
    return;
  }
  if (lca(lab, c) == lab) {
    int lbc = lca(b, c);
    int lac = lca(a, c);
    if (lvl[lbc] > lvl[lab]) {
      Ans = max(Ans, abs(lvl[lbc] - lvl[c]));
      return;
    }
    if (lvl[lac] > lvl[lab]) {
      Ans = max(Ans, abs(lvl[lac] - lvl[c]));
      return;
    }
    Ans = max(Ans, lvl[lab] - lvl[c]);
  }
  int lcc = lca(lab, c);
  Ans = max(Ans, abs(lvl[lab] - lvl[lcc]) + abs(lvl[lcc] - lvl[c]));
}
int main() {
  int k;
  scanf("%d%d", &n, &k);
  for (int i = 2; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    --x;
    g[x].push_back(i - 1);
    g[i - 1].push_back(x);
  }
  l = 1;
  while ((1 << l) <= n) ++l;
  for (int i = 0; i < n; ++i) up[i].resize(l + 1);
  dfs(0);
  while (k--) {
    int a[5];
    for (int i = 1; i <= 3; ++i) scanf("%d", a + i);
    Ans = 0;
    sort(a + 1, a + 4);
    do {
      check(a[1] - 1, a[2] - 1, a[3] - 1);
    } while (next_permutation(a + 1, a + 4));
    printf("%d\n", Ans + 1);
  }
  return 0;
}
