Farmer John wants to take photos of his $N$ cows ($2 \le N \le 10^9$), which are standing in a line and conveniently numbered $1 \ldots N$ from left to right. Each photograph can capture a consecutive range of cows from the lineup, and Farmer John wants to make sure that each cow appears in at least one photo.
Unfortunately, there are $K$ unfriendly pairs of cows ($1 \le K \le 1000$) that each refuse to be in the same photograph. Given the locations of these unfriendly pairs, determine the minimum number of photos Farmer John needs to take.
When $N = 7$ and the unfriendly pairs are $(1, 3)$, $(2, 4)$, $(5, 6)$, Farmer John can take 3 photos: