#include <bits/stdc++.h>
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define TRAV(i, a) for(auto & i : (a))
#define SZ(x) ((int)(x).size())
#define X first
#define Y second
#define MP std::make_pair
#define PR std::pair
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

int n;
std::vector<PII> A;
std::vector<PII> B;

int check(int i){
	if(A[i].X == -1 || A[i].Y == -1) return -1;
	return A[i].Y-A[i].X;
}

bool can(int a, int b){
	int len = (b-a+1);
	if(len%2 != 0) return false;
	int dl = len/2;
	FOR(i, dl){
		if(B[a+i].X == -1 && B[a+i+dl].X == -1) continue;
		if(B[a+i].Y == 0) return false;
		if(B[a+i+dl].Y == 1) return false;
		if(B[a+i].X != -1 && B[a+i+dl].X != -1 && B[a+i].X != B[a+i+dl].X) return false;
	}
	return true;
}

int main(){
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cin >> n;
	A.resize(n);
	B.resize(2*n+1, MP(-1, -1));

	std::set<int> set;
	int cnt = 0;
	FOR(i, n){
		std::cin >> A[i].X >> A[i].Y;
		if(A[i].X != -1 && A[i].Y != -1 && A[i].X >= A[i].Y){
			std::cout << "No\n";
			return 0;
		}
		if(A[i].X != -1) B[A[i].X] = MP(i, 1), set.insert(A[i].X), cnt++;
		if(A[i].Y != -1) B[A[i].Y] = MP(i, 0), set.insert(A[i].Y), cnt++;
	}
	if(SZ(set) != cnt){
		std::cout << "No\n";
		return 0;
	}
	std::vector<bool> dp(2*n+1);
	dp[0] = true;
	REP(i, 1, 2*n+1){
		FOR(j, i){
			dp[i] = (dp[i] || (dp[j] && can(j+1, i)));
		}
	}

	std::cout << (dp[2*n] ? "Yes\n" : "No\n");
	

	return 0;
}
//LIBRARIES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <chrono>
//MACROS
#define _crt_secure_no_warnings
#define FREEELO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
#define mp make_pair
#define INF 0x3F3F3F3F
#define MAX 100001
#define MAXLL 1000000000000000000LL
#define MOD 1000000007
#define LOGMAX 20
#define ALPHABET 128
#define loop(i, n) for (int i = 1; i <= (n); i++)
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define all1(v) v.begin() + 1, v.end()
#define F first
#define S second
#define EPS 1e-6
using namespace std;
//TYPE DEFINITIONS
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;
typedef unordered_set<int> usi;
typedef unordered_multiset<int> usmi;
//main exec

int main(){
    FREEELO
    string s; cin >> s; int n = sz(s); s = " " + s;
    vector<int> dp(n + 1);
    for (int i = 1; i <= n; i++){
        dp[i] = dp[i - 1] + (s[i] - '0');
    }
    if (dp[n] % 3 == 0) { cout << 0 << endl; return 0; }
    for (int k = 1; k <= n - 1; k++){
        for (int i = 1; i + k - 1 <= n; i++){
            int cur_sum = dp[i - 1] + dp[n] - dp[i + k - 1];
            if (cur_sum % 3 == 0) {
                cout << k << endl; return 0; 
            }
        }
    }
    cout << -1 << endl;
}