#include <bits/stdc++.h>
using namespace std;
const int imod = 1e9 + 7;
const long long lmod = 1e18 + 7;
const int iinf = INT_MAX;
const long long linf = LLONG_MAX;
const double pi = 2 * acos(0.0);
const double eps = 1e-7;
const int maxn = 1e5 + 1344;
bool was[maxn];
int up[maxn][20];
vector<int> g[maxn];
int timer;
bool viz[maxn];
int tin[maxn], tout[maxn];
int p[maxn];
int n, q;
int level[maxn];
void dfs(int v, int pr = -1) {
  was[v] = true;
  tin[v] = ++timer;
  up[v][0] = pr;
  for (int i = 0; i < (int)g[v].size(); ++i) {
    int to = g[v][i];
    if (!was[to]) {
      dfs(to, v);
    }
  }
  tout[v] = ++timer;
}
bool batya(int x, int y) { return tin[x] <= tin[y] && tout[x] >= tout[y]; }
int lca(int x, int y) {
  if (batya(x, y)) return x;
  if (batya(y, x)) return y;
  for (int i = 17; i >= 0; --i) {
    if (up[x][i] != -1 && !batya(up[x][i], y)) {
      x = up[x][i];
    }
  }
  return up[x][0];
}
void dfs1(int v, int len) {
  viz[v] = true;
  level[v] = len;
  for (int i = 0; i <= (int)(g[v]).size() - 1; ++i) {
    if (!viz[g[v][i]]) {
      dfs1(g[v][i], len + 1);
    }
  }
}
int get(int a, int b, int c) {
  int lca_ab = lca(a, b), lca_bc = lca(b, c), lca_ac = lca(a, c);
  int lca1;
  if (level[lca_ab] > level[lca_bc])
    lca1 = lca_ab;
  else
    lca1 = lca_bc;
  bool flag = false;
  if (level[lca1] < level[lca_ac]) flag = true;
  if (flag) return level[b] - level[lca1] + (level[lca_ac] - level[lca1]);
  return level[b] - level[lca1];
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> q;
  for (int i = 2; i <= n; ++i) {
    cin >> p[i];
    g[i].push_back(p[i]);
    g[p[i]].push_back(i);
  }
  memset(up, -1, sizeof up);
  dfs(1);
  dfs1(1, 0);
  for (int i = 1; i <= 17; ++i) {
    for (int v = 1; v <= n; ++v) {
      if (up[v][i - 1] != -1) {
        int res = up[v][i - 1];
        up[v][i] = up[res][i - 1];
      }
    }
  }
  for (int i = 1; i <= q; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    int ans = -iinf;
    ans = max(ans, get(a, b, c) + 1);
    ans = max(ans, get(b, a, c) + 1);
    ans = max(ans, get(b, c, a) + 1);
    cout << ans << endl;
  }
  return 0;
}
