#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int MAXN = 1e5 + 7;
int n, q;
int father[MAXN][20];
vector<int> head[MAXN];
int deep[MAXN];
void dfs(int u, int fa, int d) {
  deep[u] = d;
  for (auto v : head[u]) {
    if (v == fa) continue;
    father[v][0] = u;
    dfs(v, u, d + 1);
  }
}
void initLCA() {
  for (int i = 1; i <= 19; i++) {
    for (int j = 1; j <= n; j++) {
      father[j][i] = father[father[j][i - 1]][i - 1];
    }
  }
}
int lca(int a, int b) {
  if (deep[a] < deep[b]) {
    swap(a, b);
  }
  for (int i = 19; i >= 0; i--) {
    if (deep[father[a][i]] >= deep[b]) {
      a = father[a][i];
    }
  }
  if (a == b) {
    return a;
  }
  for (int i = 19; i >= 0; i--) {
    if (father[a][i] != father[b][i]) {
      a = father[a][i];
      b = father[b][i];
    }
  }
  return father[a][0];
}
int dis(int u, int v) { return deep[u] + deep[v] - 2 * deep[lca(u, v)]; }
int main() {
  scanf("%d%d", &n, &q);
  int x;
  for (int i = 2; i <= n; ++i) {
    scanf("%d", &x);
    head[x].push_back(i);
    head[i].push_back(x);
  }
  dfs(1, 0, 0);
  initLCA();
  int a, b, c;
  while (q--) {
    scanf("%d%d%d", &a, &b, &c);
    int l1 = lca(a, b);
    int l2 = lca(a, c);
    int l3 = lca(b, c);
    if (deep[l2] > deep[l1]) l1 = l2;
    if (deep[l3] > deep[l1]) l1 = l3;
    printf("%d\n", max(dis(l1, a), max(dis(l1, b), dis(l1, c))) + 1);
  }
  return 0;
}
