1. Abridged problem statement  
Given a grid with N rows and M columns, Petya stands at the bottom-left cell. Every other cell contains an enemy. A single shot from Petya’s position kills all enemies lying on the same straight line. Compute the minimum number of shots needed to eliminate all enemies.

2. Detailed editorial  

Problem restatement and reduction  
- Label Petya’s cell as (1,1). Every other cell has coordinates (i,j) with 1≤i≤N, 1≤j≤M, and (i,j)≠(1,1).  
- A shot in direction (Δi,Δj) destroys every enemy whose offset from Petya is an integer multiple of (Δi,Δj).  
- Thus distinct shots correspond one-to-one with primitive direction vectors (dx,dy) where dx=i−1, dy=j−1, not both zero, and gcd(dx,dy)=1.  

We need to count how many integer pairs (dx,dy) satisfy  
  0≤dx≤N−1, 0≤dy≤M−1, (dx,dy)≠(0,0), gcd(dx,dy)=1.  

Breakdown  
A. Horizontal and vertical shots  
  - dx=0, dy>0 → only dy=1 is primitive ⇒ one vertical shot (if M>1).  
  - dy=0, dx>0 → only dx=1 is primitive ⇒ one horizontal shot (if N>1).  

B. Shots at non-axis directions  
  - Count pairs with dx≥1 and dy≥1, gcd(dx,dy)=1.  
  - Total such pairs = ∑_{dx=1..N−1} ∑_{dy=1..M−1} [gcd(dx,dy)=1].  

Directly iterating over dx,dy up to 10^6×10^6 is impossible. Use a divisor-based sieve (inclusion–exclusion via Möbius inversion) to count the number of coprime pairs efficiently:  

Let A = N−1, B = M−1.  
Let f(d) = number of pairs (dx,dy) with dx≥1, dy≥1, gcd(dx,dy)=d.  
  - f(d) = floor(A/d) * floor(B/d).  
We want g = f(1), but f(1) counts all pairs including higher-gcd ones. By standard Möbius-style inversion (or by subtracting multiples), we compute for d from min(A,B) down to 1:  
 1) dp[d] = f(d) − ∑_{k≥2} dp[k·d].  
 2) g = dp[1].  

Final answer = g + (N>1 ? 1 : 0) + (M>1 ? 1 : 0).  

Complexity  
 O(min(N,M) · log(min(N,M))) by iterating divisors, which is fine up to 10^6.

3. Provided C++ solution with detailed comments  

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

// Overload << for pairs
template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

// Overload >> for pairs
template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

// Overload >> for vectors
template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

// Overload << for vectors
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, m;

// Read input N and M
void read() {
    cin >> n >> m;
}

void solve() {
    // A = number of steps in vertical direction = n−1
    // B = number of steps in horizontal direction = m−1
    // Initial count of all dx>=1, dy>=1 pairs (not necessarily coprime)
    int64_t ans = (int64_t)(n - 1) * (m - 1);

    // Let lim = min(n−1, m−1). We allocate dp[0..lim].
    // dp[d] will end up holding the count of pairs (dx,dy) with gcd(dx,dy)==d.
    int lim = min(n, m) - 1;
    if (lim < 1) lim = 1;  // avoid zero size
    vector<int64_t> dp(lim + 1, 0);

    // We process d from high to low to apply inclusion–exclusion:
    // f(d) = floor((n-1)/d)*floor((m-1)/d)
    // dp[d] = f(d) - sum_{k>=2} dp[k*d]
    for(int d = lim; d >= 2; d--) {
        // count of pairs where both are multiples of d
        dp[d] = (int64_t)((n - 1) / d) * ((m - 1) / d);
        // subtract those pairs already counted for higher multiples
        for(int kd = 2 * d; kd <= lim; kd += d) {
            dp[d] -= dp[kd];
        }
        // remove dp[d] from ans since those are not gcd=1
        ans -= dp[d];
    }

    // ans now holds the number of pairs (dx, dy), dx>=1, dy>=1, gcd(dx,dy)=1.
    // plus one shot for horizontal (dx=1, dy=0) if n>1
    // plus one shot for vertical   (dx=0, dy=1) if m>1
    int extra = 0;
    if (n > 1) extra++;
    if (m > 1) extra++;

    cout << ans + extra << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    read();
    solve();
    return 0;
}
```

4. Python solution with detailed comments  

```python
import sys
import threading

def main():
    data = sys.stdin.read().split()
    n, m = map(int, data)
    # A = n-1, B = m-1
    A, B = n - 1, m - 1

    # Count all pairs dx>=1, dy>=1 unconstrained by gcd
    total_pairs = A * B

    # We will build dp[d] = number of pairs (dx,dy) with gcd(dx,dy)==d
    # for d from 1..lim, but we only need d>=2 to subtract non-coprime.
    lim = min(A, B)
    dp = [0] * (lim + 1)

    # Inclusion–exclusion from largest d down to 2
    for d in range(lim, 1, -1):
        # pairs where dx and dy share factor d
        cnt = (A // d) * (B // d)
        # subtract those already attributed to multiples of d
        multiple = 2 * d
        while multiple <= lim:
            cnt -= dp[multiple]
            multiple += d
        dp[d] = cnt
        total_pairs -= cnt  # remove all non-coprime pairs

    # total_pairs now equals count of dx>=1,dy>=1,gcd=1
    # plus horizontal shot if n>1 (dx>0,dy=0) and vertical if m>1
    shots = total_pairs
    if n > 1:
        shots += 1
    if m > 1:
        shots += 1

    print(shots)

if __name__ == "__main__":
    threading.Thread(target=main).start()
```

5. Compressed editorial  
Count primitive direction vectors from (1,1) to every enemy cell. Axis directions give up to two shots. Non-axis directions correspond to all integer pairs (dx,dy) with 1≤dx≤N−1, 1≤dy≤M−1 and gcd(dx,dy)=1. Use a divisor sieve: for each d from min(N−1,M−1) down to 2 compute dp[d]=⌊(N−1)/d⌋·⌊(M−1)/d⌋−∑_{k≥2}dp[kd]. Subtract these non-coprime counts from total (N−1)(M−1) to get coprime pairs. Add the two axis shots if applicable. Total complexity O(min(N,M)·log min(N,M)).