#include <bits/stdc++.h>
using namespace std;

struct TreeNode{
	int val;
	TreeNode* right;
	TreeNode* left;
	
	TreeNode(int val) : left(nullptr),right(nullptr),val(val){};
};
TreeNode* lca(TreeNode* root,TreeNode* p,TreeNode* q){
	if(root == nullptr)return nullptr;
	if(p == root || q == root)return root;
	
	TreeNode* left = lca(root->left,p,q);
	TreeNode* right = lca(root->right,p,q);
	if(left != nullptr && right != nullptr) {
        return root;
    }
	return left == nullptr?right:left;
}


TreeNode* buildTree(){
	int x;
	cin>>x;
	if(x==-1)return nullptr;
	
	TreeNode* root = new TreeNode(x);
	
	queue<TreeNode*>q;
	
	q.push(root);
	
	while(!q.empty()){
		auto u = q.front();q.pop();
		
		if(cin>>x && x!=-1){
			u->left = new TreeNode(x);
			q.push(u->left);
		}
		
		if(cin>>x && x!=-1){
			u->right = new TreeNode(x);
			q.push(u->right);
		}
	}
	return root;
}
TreeNode* find(TreeNode* root,int val){
	if(!root)return nullptr;
	if(root->val == val)return root;
	
	TreeNode* left = find(root->left,val);
	
	if(left)return left;
	return find(root->right,val);
}
int main() {
    TreeNode* root = buildTree();
   int val1,val2; cin>>val1 >>val2;
    TreeNode* p = find(root,val1);
    TreeNode* q = find(root,val2);
    
    TreeNode* res = lca(root,p,q);
   if(res) {
        cout << res->val << endl;
    } else {
        cout << "Error: Could not find the nodes. Check your input!" << endl;
    }
    
	return 0;
}