// Starcraft 2 enjoyer //
#include <bits/stdc++.h>
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
#define LSOne(X) ((X) & -(X))
const int N = 1e5 + 5;
const int M = 1 << 15;
const int B = 18;
const long long K = 2;
const int LG = 20;
const int INF = 1e9 + 5;
const int P = 31;
const int MOD = 1e9 + 7;
const int nx[] = {0, 1, 0, -1}, ny[] = {-1, 0, 1, 0};
int n, q, sz[N], rem[N], root, mn[N], pos[N], binlift[LG][N], val[N], depth[N], id[N];
vector<int> adj[N], tour;
priority_queue<int, vector<int>, greater<int>> pq;
bool cmp(int a, int b)
{
return mn[a] > mn[b];
}
void dfs1(int c)
{
mn[c] = c;
for (int i : adj[c])
{
depth[i] = depth[c] + 1;
binlift[0][i] = c;
for (int x = 1; x < LG; x++)
{
binlift[x][i] = binlift[x - 1][binlift[x - 1][i]];
}
dfs1(i);
mn[c] = min(mn[c], mn[i]);
}
}
void dfs2(int c)
{
pos[n - 1 - tour.size()] = c;
id[c] = tour.size();
tour.push_back(c);
sort(adj[c].begin(), adj[c].end(), cmp);
for (int i : adj[c])
{
dfs2(i);
}
}
inline void solve()
{
cin >> n >> q;
for (int x = 1; x <= n; x++)
{
int i;
cin >> i;
if (i == 0)
{
root = x;
}
else
{
adj[i].push_back(x);
// cout << i << " " << x << "\n";
}
pq.push(x - 1);
val[x] = 0;
}
dfs1(root);
dfs2(root);
reverse(tour.begin(), tour.end());
// for (int i : tour)
// cout << i << " ";
// cout << "\n";
for (int x = 0; x < q; x++)
{
int t, v;
cin >> t >> v;
if (t == 1)
{
int i;
for (int y = 0; y < v; y++)
{
// cout << pos[pq.top()] << " ";
val[pos[pq.top()]] = 1;
i = pq.top();
pq.pop();
}
cout << pos[i] << "\n";
}
else
{
int i = v;
for (int y = LG - 1; y >= 0; y--)
{
if (val[binlift[y][v]] == 1)
{
v = binlift[y][v];
}
}
pq.push(id[v]);
val[v] = 0;
// cout << i << " " << v << "\n";
cout << depth[i] - depth[v] << "\n";
}
}
}
signed main()
{
// freopen("CP.INP", "r", stdin);
// freopen("CP.OUT", "w", stdout);
// freopen("CGRAPH.INP", "r", stdin);
// freopen("CGRAPH.OUT", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
for (int x = 1; x <= t; x++)
{
solve();
}
return 0;
}