#include <bits/stdc++.h>
using namespace std;
const int size1 = 2e5 + 500;
const int INF = 1e9;
vector<vector<int> > graph(size1);
static int binup[size1][21];
static int d[size1];
int t = 0;
static int tin[size1], tout[size1];
static bool used[size1];
void dfs(int v, int depth) {
  t++;
  tin[v] = t;
  used[v] = true;
  d[v] = depth;
  for (int i = 0; i < graph[v].size(); i++) {
    if (!used[graph[v][i]]) {
      dfs(graph[v][i], depth + 1);
    }
  }
  t++;
  tout[v] = t;
}
int lca(int v, int u) {
  if (d[v] < d[u]) {
    swap(v, u);
  }
  for (int i = 20; i >= 0; i--) {
    if (d[v] - (1 << i) >= d[u]) {
      v = binup[v][i];
    }
  }
  if (v == u) {
    return v;
  }
  for (int i = 20; i >= 0; i--) {
    if (binup[v][i] != binup[u][i]) {
      v = binup[v][i];
      u = binup[u][i];
    }
  }
  return binup[v][0];
}
int f(int s, int f, int t) {
  int pr = lca(s, f);
  int pr1 = lca(f, t);
  if (d[pr1] < d[pr]) {
    return d[f] - d[pr] + 1;
  }
  if (d[pr1] > d[pr]) {
    return d[f] - d[pr1] + 1;
  }
  int pr2 = lca(s, t);
  return d[pr2] - d[pr] + d[f] - d[pr] + 1;
}
int main() {
  int n, q;
  cin >> n >> q;
  int i;
  static int p[size1];
  p[1] = 1;
  for (i = 2; i <= n; i++) {
    cin >> p[i];
    graph[p[i]].push_back(i);
    graph[i].push_back(p[i]);
  }
  for (i = 1; i <= n; i++) {
    binup[i][0] = p[i];
  }
  int j;
  for (i = 1; i <= 20; i++) {
    for (j = 1; j <= n; j++) {
      binup[j][i] = binup[binup[j][i - 1]][i - 1];
    }
  }
  dfs(1, 0);
  for (i = 1; i <= q; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    cout << max(f(a, b, c),
                max(f(a, c, b),
                    max(f(b, a, c),
                        max(f(b, c, a), max(f(c, a, b), f(c, b, a))))))
         << "\n";
  }
  return 0;
}
