<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

115. Calendar

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


First year of new millenium is gone away. In commemoration of it write a program that finds the name of the day of the week for any date in 2001.


Input

Input is a line with two positive integer numbers N and M, where N is a day number in month M. N and M is not more than 100.


Output

Write current number of the day of the week for given date (Monday – number 1, … , Sunday – number 7) or phrase “Impossible” if such date does not exist.


Sample Input

21 10
Sample Output

7
Author	: Michael R. Mirzayanov
Resource	: PhTL #1 Training Contests
Date	: January 2002

<|response|>
1. Abridged Problem Statement  
Given two integers N and M representing a date N-th day of month M in the year 2001, output the weekday number (Monday=1, …, Sunday=7). If the date is not valid (month not in 1…12 or day exceeds that month’s length), print “Impossible”.

2. Key Observations  
• 2001 is not a leap year, so month lengths are fixed:  
  January=31, February=28, March=31, April=30, …, December=31.  
• January 1, 2001 was a Monday.  
• If you count days elapsed since Jan 1 (zero-based), day D after Jan 1 falls on weekday index ((D mod 7) + 1).  
  – D=0 → Monday(1), D=1 → Tuesday(2), …, D=6 → Sunday(7), then it repeats.

3. Full Solution Approach  
Step 1: Read integers N (day) and M (month).  
Step 2: Validate:  
  – 1 ≤ M ≤ 12  
  – 1 ≤ N ≤ days_in_month[M]  
  If invalid, print “Impossible” and exit.  
Step 3: Compute days elapsed since Jan 1 in zero-based form:  
  D = sum of lengths of months 1 to M−1  +  (N − 1)  
Step 4: Compute weekday = (D mod 7) + 1  
Step 5: Print weekday.

This runs in O(1) time and uses constant space.

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

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

    int N, M;
    cin >> N >> M;
    // Month lengths for 2001 (non-leap year)
    vector<int> monthlen = {31,28,31,30,31,30,31,31,30,31,30,31};

    // Validate month and day
    if (M < 1 || M > 12 || N < 1 || N > monthlen[M-1]) {
        cout << "Impossible\n";
        return 0;
    }

    // Compute zero-based day offset since Jan 1
    int D = 0;
    // Add full months before month M
    for (int m = 1; m < M; ++m) {
        D += monthlen[m-1];
    }
    // Add days within month M, shifting Jan 1 to D=0
    D += (N - 1);

    // Weekday index: D=0 → Monday=1, …, D=6 → Sunday=7
    int weekday = (D % 7) + 1;
    cout << weekday << "\n";
    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys

def main():
    data = sys.stdin.read().split()
    if len(data) < 2:
        return
    N, M = map(int, data)

    # Month lengths for 2001
    monthlen = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    # Validate input
    if M < 1 or M > 12 or N < 1 or N > monthlen[M-1]:
        print("Impossible")
        return

    # Compute days elapsed since Jan 1 in zero-based form
    days_elapsed = sum(monthlen[:M-1]) + (N - 1)

    # Compute weekday: Monday=1 … Sunday=7
    weekday = (days_elapsed % 7) + 1
    print(weekday)

if __name__ == "__main__":
    main()
```