#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 1e3;
const int MAXL = 20;
vector<int> G[MAXN];
int n, m, d[MAXN];
int anc[MAXN][MAXL];
void dfs(int u, int pa) {
  for (int i = 0; i < G[u].size(); ++i) {
    int v = G[u][i];
    if (v == pa) continue;
    d[v] = d[u] + 1;
    anc[v][0] = u;
    dfs(v, u);
  }
}
void swim(int& u, int h) {
  for (int j = MAXL - 1; j > -1 && h; --j)
    if (1 << j <= h) {
      u = anc[u][j];
      h -= 1 << j;
    }
}
int findLCA(int u, int v) {
  if (d[u] < d[v]) swap(u, v);
  if (d[u] != d[v]) swim(u, d[u] - d[v]);
  while (u != v) {
    int i = 0;
    while (anc[u][i] != anc[v][i]) ++i;
    if (i == 0) return anc[u][i];
    u = anc[u][i - 1];
    v = anc[v][i - 1];
  }
  return u;
}
int cal(int s, int f, int t) {
  int sf = findLCA(s, f);
  int ft = findLCA(f, t);
  if (sf != ft) {
    if (d[sf] > d[ft]) ft = sf;
    return d[f] - d[ft] + 1;
  }
  return d[f] + d[findLCA(s, t)] - 2 * d[sf] + 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 2; i <= n; ++i) {
    int v;
    scanf("%d", &v);
    G[i].push_back(v);
    G[v].push_back(i);
  }
  d[1] = 0;
  anc[1][0] = 1;
  dfs(1, -1);
  for (int i = 1; i < MAXL; ++i)
    for (int j = 1; j <= n; ++j) anc[j][i] = anc[anc[j][i - 1]][i - 1];
  while (m--) {
    int v[3];
    scanf("%d%d%d", v, v + 1, v + 2);
    int ans = 0;
    for (int i = 0; i < 3; ++i) {
      int tmp = cal(v[i], v[(i + 1) % 3], v[(i + 2) % 3]);
      ans = max(ans, tmp);
    }
    printf("%d\n", ans);
  }
  return 0;
}
