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

using ll = long long;

struct TreeNode{
	int val;
	TreeNode* left;
	TreeNode* right;
	
	TreeNode(int val):val(val),left(nullptr),right(nullptr){};
};

bool sum(TreeNode* root){
  if(!root)return true;
  
  if(!root->left && !root->right)return true;
  
  int left = root->left? root->left->val : 0;
  int right = root->right? root->right->val : 0;
  
  return (root->val == left+right)&&sum(root->left)&&sum(root->right);
}
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;
}
int main() {
TreeNode* root = buildTree();
bool ans = sum(root);
cout<<ans<<endl;
	return 0;
}