#include <bits/stdc++.h>
using namespace std;
int h[1000005], pa[1000005][21];
bool ok[1000005];
vector<int> e[1000005];
void dfs(int s, int d) {
  ok[s] = 1;
  h[s] = d;
  for (int i = 1; i <= 20; ++i) {
    pa[s][i] = pa[pa[s][i - 1]][i - 1];
  }
  for (int v : e[s]) {
    if (ok[v]) continue;
    pa[v][0] = s;
    dfs(v, d + 1);
  }
}
int lca(int x, int y) {
  if (h[x] < h[y]) swap(x, y);
  int d = h[x] - h[y];
  for (int i = 0; i <= 20; ++i) {
    if (d >> i & 1) x = pa[x][i];
  }
  for (int i = 20; i >= 0; --i) {
    if (pa[x][i] == pa[y][i]) continue;
    x = pa[x][i];
    y = pa[y][i];
  }
  if (x != y) x = pa[x][0];
  return x;
}
int test(int x, int y, int z) {
  int d1 = h[x];
  int d2 = h[lca(x, y)];
  int d3 = h[lca(x, z)];
  int d5 = max(d2, d3);
  int d4 = d5;
  if (d2 == d3) {
    d4 = h[lca(y, z)];
  }
  return d1 - d5 + 1 + (d4 - d5);
}
int main() {
  int n, m, x, y, z;
  cin >> n >> m;
  for (int i = 2; i <= n; ++i) {
    cin >> x;
    e[x].push_back(i);
  }
  dfs(1, 0);
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y >> z;
    cout << max(test(x, y, z), max(test(y, z, x), test(z, x, y))) << '\n';
  }
  return 0;
}
