2-SAT Satisfiability

No attempts yetTime limit1sMemory limit256 MB

Problem

2-SAT asks whether boolean variables x1,x2,,xNx_1, x_2, \ldots, x_N can be assigned values that make a 2-CNF formula true.

A 2-CNF formula looks like (xy)(¬yz)(x¬z)(zy)(x \lor y) \land (\lnot y \lor z) \land (x \lor \lnot z) \land (z \lor y). Each parenthesized part is a clause, and a clause is two variables joined by \lor. Here \lor is OR, \land is AND, and ¬\lnot is NOT.

Given the number of variables NN, the number of clauses MM, and the formula ff, write a program that decides whether ff can be made true.

For example, take N=3N = 3, M=4M = 4, and f=(¬x1x2)(¬x2x3)(x1x3)(x3x2)f = (\lnot x_1 \lor x_2) \land (\lnot x_2 \lor x_3) \land (x_1 \lor x_3) \land (x_3 \lor x_2). Setting x1x_1 to false, x2x_2 to false, and x3x_3 to true makes ff true. With N=1N = 1, M=2M = 2, and f=(x1x1)(¬x1¬x1)f = (x_1 \lor x_1) \land (\lnot x_1 \lor \lnot x_1), no value of x1x_1 makes ff true.

Input

The first line has the number of variables NN (1N201 \le N \le 20) and the number of clauses MM (1M1001 \le M \le 100). Each of the next MM lines has one clause, given as two integers ii and jj (1i,jN1 \le |i|, |j| \le N). A positive integer means xix_i or xjx_j, and a negative integer means ¬xi\lnot x_{-i} or ¬xj\lnot x_{-j}.

Output

Print 1 on the first line if ff can be made true, and 0 otherwise.