#include #include #include #include #include using namespace std; template class y_combinator_result { Fun fun_; public: template explicit y_combinator_result(T &&fun): fun_(std::forward(fun)) {} template decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward(args)...); } }; template decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result>(std::forward(fun)); } void solve() { int n, m; cin >> n >> m; vector> children(n); for(int i = 1; i < n; i++) { int a, b; cin >> a >> b; children[--a].push_back(--b); } for(int i = 0; i < n; i++) sort(children[i].begin(), children[i].end()); vector lhst(n, -1), rhst(n, -1); { int ct = 0; auto dfs = y_combinator([&](auto self, int curr) -> void { lhst[curr] = ct++; for(int out: children[curr]) self(out); rhst[curr] = ct++; }); dfs(0); } vector ord(m); for(auto& x: ord) { cin >> x; x--; } int nowt = 0; int ans = 0; for(int curr: ord) { if(nowt > rhst[curr]) { cout << ans << "\n"; return; } nowt = max(nowt, lhst[curr]); ans++; } cout << ans << "\n"; } int main() { solve(); }