#include <bits/stdc++.h>
using namespace std;
int64_t inp() {
  int64_t x;
  cin >> x;
  return x;
}
string inp_str() {
  string x;
  cin >> x;
  return x;
}
void fast_io() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
void file_io(const char *in, const char *out) {
  freopen(in, "r", stdin);
  freopen(out, "w", stdout);
}
void print_bool(bool a, string t = "YES", string f = "NO") {
  if (a)
    cout << t << "\n";
  else
    cout << f << "\n";
}
vector<int64_t> a(3), ans(3);
int64_t n, q, m;
const int64_t N = 1e5 + 10;
vector<int64_t> adj[N];
int64_t h[N], euler[5 * N], tree[10 * N], pos[N];
void Input() {
  cin >> n >> q;
  for (int64_t i = (2); i <= (n); ++i) {
    adj[inp()].push_back(i);
  }
}
void dfs(int64_t u, int64_t H = 0) {
  pos[u] = m;
  euler[m++] = u;
  h[u] = H;
  for (auto &v : adj[u]) {
    dfs(v, h[u] + 1);
    euler[m++] = u;
  }
}
void buildST(int64_t p = 1, int64_t b = 0, int64_t e = m - 1) {
  if (b == e) {
    tree[p] = euler[b];
    return;
  }
  int64_t mid = (e - b) / 2 + b;
  buildST(p * 2, b, mid);
  buildST(p * 2 + 1, mid + 1, e);
  int64_t u1 = tree[2 * p], u2 = tree[2 * p + 1];
  tree[p] = (h[u1] < h[u2]) ? u1 : u2;
}
int64_t rmq(int64_t L, int64_t R, int64_t p = 1, int64_t b = 0,
            int64_t e = m - 1) {
  if (e < L || R < b) return -1;
  if (L <= b && e <= R) return tree[p];
  int64_t mid = (e - b) / 2 + b;
  int64_t u1 = rmq(L, R, p * 2, b, mid);
  int64_t u2 = rmq(L, R, p * 2 + 1, mid + 1, e);
  if (u1 == -1) return u2;
  if (u2 == -1) return u1;
  return (h[u1] < h[u2]) ? u1 : u2;
}
int64_t lca(int64_t u, int64_t v) {
  if (pos[u] > pos[v]) swap(u, v);
  return rmq(pos[u], pos[v]);
}
int64_t dist(int64_t u, int64_t v) { return h[u] + h[v] - 2 * h[lca(u, v)]; }
void Solve() {
  m = 0;
  dfs(1);
  buildST();
  while (q--) {
    generate((a).begin(), (a).end(), inp);
    sort((a).begin(), (a).end());
    int64_t best = 0;
    while (1) {
      int64_t p1 = lca(a[0], a[1]), p2 = lca(a[0], a[2]);
      if (h[p1] != h[p2])
        best = max(best, h[a[0]] - max(h[p2], h[p1]) + 1);
      else {
        int64_t p = lca(a[1], a[2]);
        best = max(best, dist(p, a[0]) + 1);
      }
      if (!next_permutation((a).begin(), (a).end())) break;
    }
    cout << best << "\n";
  }
}
int main() {
  fast_io();
  Input(), Solve();
  return 0;
}
