We need to determine if there exists a non-leap year (i.e., a common year with 365 days, not leap) such that the first day of some month (call it month M) is a certain day D1, and the first day of the next month (M+1) is D2. We need to check if such a pair of consecutive months can occur in some common year.
We are given two day names (strings). We need to output YES if there exists some non-leap year and some month M (1..12, but M=12 would have next month? Actually "next month" presumably means the following month in the same year; but M=12's next month would be January of next year, which would not be in the same year. So M must be from 1 to 11 inclusive. So we only consider consecutive months within the same year). So we have months with lengths: Jan (31), Feb (28), Mar (31), Apr (30), May (31), Jun (30), Jul (31), Aug (31), Sep (30), Oct (31), Nov (30), Dec (31). The next month after a month M has length L_M. The day of week shift from the first of month M to the first of month M+1 is congruent to (L_M mod 7) days forward. Because if month M has L days, then the first day of month M+1 is exactly L days after the first day of month M (since day 1 to next month day 1). So shift = L_M mod 7.
Thus for a given pair of days (D1, D2), we need to check if there exists a month M (1..11) such that shifting D1 by (days in month M mod 7) yields D2. But also we need to ensure that there exists some common year where the first of month M is D1. Is it always possible for any month to have any day-of-week as its first day? In a given common year, the pattern of first days is determined by the day of week of Jan 1. But we are not fixing the year; we can choose any common year (i.e., any year that is not leap; but also we could consider any year that has 365 days and follows Gregorian calendar, but note that Gregorian calendar has rules for leap years: years divisible by 4 except centuries not divisible by 400. But we are only considering non-leap years. So essentially we consider any year that is not a leap year, so Jan 1 can be any day of the week? In Gregorian calendar, the day of week for Jan 1 repeats with period 400 years. There are 97 leap years in 400 years, so total days mod 7 = 400365 + 97 = 146097 days which is divisible by 7? 146097 / 7 = 20871 exactly (since 720871 = 146097). So the cycle is 400 years. Over that cycle, each day of week occurs equally often for Jan 1? Probably yes. So for non-leap years, there are 303 non-leap years in 400 years. Jan 1 can be any day of the week in some non-leap year? Possibly yes. But we need to be careful: If we just need existence of some year (non-leap) such that for a chosen month M, its first day is D1, then we can pick the year accordingly. However, the first days of months are determined by Jan 1. If we can choose Jan 1 arbitrarily among days of week? For any day of week, there exists a non-leap year with Jan 1 on that day? I think yes. Because the distribution is uniform, but we need to confirm: Over the 400-year cycle, the number of non-leap years that start with Monday etc. I suspect each day occurs some number of times, and likely all days appear. Even if one day doesn't appear as Jan 1 in a non-leap year, but I think all appear. Let's check: In Gregorian calendar, the day of week for Jan 1 cycles through all possibilities. Since the cycle length is 400 years and the total days mod 7 = 0, the sequence of Jan 1 days repeats every 400 years. Within that, there are leap and non-leap years. But we can check quickly: For any given day D, is there a non-leap year with Jan 1 = D? Probably yes. Example: 2015 is a non-leap year? 2015 is not divisible by 4, so non-leap, and Jan 1 2015 was Thursday. 2016 was leap, 2017 non-leap and Jan 1 2017 was Sunday? Actually Jan 1 2017 was Sunday. 2018 Monday? Jan 1 2018 was Monday. 2019 Tuesday, 2020 leap, 2021 Friday? Wait Jan 1 2021 was Friday. So we have many days. So it seems plausible that for each day, there is some non-leap year. So existence of year for a given Jan 1 day is not an issue. But we need to consider month M. For a given Jan 1, the first of month M is determined. So we need to know: For which pairs (M, D1) does there exist a common year such that the first of month M is D1? Equivalent to: For given M, can the first of month M be any day of the week in some common year? Possibly yes, but we should verify. Because the mapping from Jan 1 to first of month M is adding a fixed offset (the total days in months Jan through M-1 mod 7). That offset is deterministic given common year lengths. But for a common year, the offset for month M is the same for all common years? Actually the offset from Jan 1 to first of month M is the sum of lengths of months Jan to M-1 (all 31, 28, etc) modulo 7. However, note: if the year is leap, February has 29 days, but we are only considering non-leap years, so February has 28 days. So the offset for month M in a common year is fixed: it's the cumulative sum of days in previous months (mod 7). So for a given M, the first of month M is just (Jan 1 + offset_M) mod 7, where offset_M = sum_{i=1}^{M-1} days_i mod 7, with days: Jan=31, Feb=28, Mar=31, Apr=30, May=31, Jun=30, Jul=31, Aug=31, Sep=30, Oct=31, Nov=30, Dec=31. Actually careful: For M=1, offset_1 = 0. For M=2 (February), offset = Jan days = 31 mod 7 = 3. So Feb 1 = Jan 1 + 3 mod 7. For M=3 (March), offset = Jan+Feb = 31+28=59 mod 7 = 59 mod 7 = 3 (since 56 is 87, remainder 3) Actually 31+28=59, 59-56=3. So offset = 3. But wait earlier we had Jan offset 0, Feb offset 3, March offset 3? Let's compute properly: Jan: 0; Feb: 31 mod7 = 3; Mar: 31+28=59, 59 mod7=3 (since 78=56, remainder 3); Apr: 31+28+31=90, 90 mod7=6 (since 84=127, remainder 6); May: +30=120, 120 mod7=1 (since 119=177, remainder 1); Jun: +31=151, 151 mod7=4 (since 147=217, remainder 4); Jul: +30=181, 181 mod7=6 (since 175=257, remainder 6); Aug: +31=212, 212 mod7=2 (since 210=307, remainder 2); Sep: +31=243, 243 mod7=5 (since 238=347, remainder 5); Oct: +30=273, 273 mod7=0 (since 273=397 exactly); Nov: +31=304, 304 mod7=3 (since 301=437, remainder 3); Dec: +30=334, 334 mod7=5 (since 329=47*7, remainder 5). So offsets: 1:0, 2:3, 3:3, 4:6, 5:1, 6:4, 7:6, 8:2, 9:5, 10:0, 11:3, 12:5. Double-check: For October, offset sum of days Jan-Sep = let's recalc precisely: Jan 31, Feb 28, Mar 31, Apr 30, May 31, Jun 30, Jul 31, Aug 31, Sep 30 = sum = 31+28=59, +31=90, +30=120, +31=151, +30=181, +31=212, +31=243, +30=273. 273 mod 7 = 273/7=39 exactly. Yes, offset 0. So Oct 1 = Jan 1. That's interesting.
So for any common year, the day-of-week for first of month M is determined by Jan 1. Thus the set of possible first days for a given month M, over all choices of Jan 1 (i.e., over all non-leap years), is just all 7 days? Because as Jan 1 ranges over all 7 days, (Jan 1 + offset_M) mod 7 also ranges over all 7 days (since offset_M is fixed). So yes, for each month M, there exists some common year where its first day is any given D1. We just need to set Jan 1 = D1 - offset_M mod 7, and check that such a year (non-leap) exists with that Jan 1. But we argued that for any desired Jan 1 day, there is some non-leap year. However, is that always true? Let's confirm: In Gregorian calendar, the day-of-week for Jan 1 cycles through all days over 400 years. Among the 400 years, exactly 97 are leap years and 303 are non-leap years. The distribution of Jan 1 among non-leap years is not necessarily uniform? But given that there are 303 non-leap years, and 7 days, by pigeonhole principle each day appears at least floor(303/7)=43 times, but could some day be missing? Possibly not; I think all days appear. But let's verify: The pattern of leap years and day-of-week advances by 1 for common year, 2 for leap year. Starting from some reference, we can generate the sequence. But I suspect that all days appear as Jan 1 for some non-leap year. However, to be absolutely sure, we might not need to worry because the problem likely expects a simpler reasoning: Since we can choose any year, we can treat Jan 1 as arbitrary. In typical Codeforces problem (I recall this is from Codeforces Round #... Actually it's a known problem "Days of Week" maybe?), the solution is to simply check if there exists a month M (1..11) such that days between first days differ by (month length mod 7) equal to the given difference. Because we can always choose a year accordingly. So the answer is YES if the difference (number of days from D1 to D2 forward) modulo 7 equals any of the month lengths mod 7 for months 1..11. Since month lengths: 31,28,31,30,31,30,31,31,30,31,30,31. For consecutive months, we consider the length of the first month (the month we start with) because shift from its first to next month's first equals its number of days mod 7. So we need to check if there is a month M (1..11) such that (L_M mod 7) equals (distance from D1 to D2) mod 7. But careful: The direction: D1 -> after L days -> D2. So if we represent days as numbers 0..6 (e.g., Monday=0, Tuesday=1, etc), then D2 = (D1 + L_M) mod 7. So we need (D2 - D1) mod 7 = L_M mod 7. So we need to see if the difference (mod 7) is among the set of offsets {31%7=3, 28%7=0, 31%7=3, 30%7=2, 31%7=3, 30%7=2, 31%7=3, 31%7=3, 30%7=2, 31%7=3, 30%7=2, 31%7=3} but only for months 1..11 (since we need next month exists). Actually we need L_M mod 7 for M = Jan (31) ->3, Feb (28)->0, Mar (31)->3, Apr (30)->2, May (31)->3, Jun (30)->2, Jul (31)->3, Aug (31)->3, Sep (30)->2, Oct (31)->3, Nov (30)->2. Dec is not considered as M because no next month within year? The problem says "first day of some month" and "first day of the next month" while both months should belong to one year. That implies the next month must also be in the same year. So M cannot be December. So we consider M from 1 to 11 inclusive.
Thus possible shift values: 0 (February), 2 (April, June, September, November), 3 (January, March, May, July, August, October). That is, shifts: 0,2,3. So the difference (D2 - D1) mod 7 must be 0, 2, or 3.
But wait, what about shift = something else? Let's check month lengths: 31%7=3, 28%7=0, 30%7=2. Yes only these three.
So the answer is YES if the number of days between D1 and D2 (in forward direction) modulo 7 equals 0, 2, or 3. But we also must check that D1 can be the first day of that month in some common year. As argued, for any month and any D1, we can find a common year where that month starts on D1 (by setting Jan 1 appropriately). However, there might be a subtlety: For February (length 28, shift 0), if we want the first of February to be D1 and first of March to be D2, then we require D2 = D1 (since shift 0). So if D1=D2, then February works. But we must also be able to have a non-leap year where Feb 1 = D1 and March 1 = D1. In a non-leap year, February has 28 days (which is 0 mod 7), so indeed if Feb 1 = X, then March 1 = X (since 28 days later is same day). So that's fine. But does there exist a non-leap year with Feb 1 = some specific day? Yes.
But there might be a nuance: The condition "both months should belong to one year." It says "first day of some month was equal to the first day of the week you are given, while the first day of the next month was equal to the second day of the week you are given." So we need existence of some year (non-leap) and some month M (1..11) such that first_day(M) = D1 and first_day(M+1) = D2. So we need to check if such a pair (M, year) exists.
Given that for each M, the relationship between first_day(M) and first_day(M+1) is fixed shift = L_M mod 7. So the existence reduces to: is there an M such that shift = (D2 - D1) mod 7? However, we also need that there is a year where first_day(M) = D1 (or equivalently first_day(M+1) = D2). But if there is an M with shift matching, can we always find a year with first_day(M)=D1? Since first_day(M) can be any day as Jan 1 varies, yes. But wait: For a fixed M, the set of possible (first_day(M), first_day(M+1)) pairs is not all pairs with shift = L_M mod 7? Actually if we can choose Jan 1 arbitrarily among all 7 days, then first_day(M) takes all 7 values, and first_day(M+1) = first_day(M) + L_M mod 7. So the set of pairs is exactly all pairs (x, x+L_M mod 7) for x in 0..6. So for any D1, we can set x=D1, then D2 would be D1+L_M mod 7. Therefore if (D2 - D1) mod 7 = L_M, then there exists a year (by setting Jan 1 appropriately) such that first_day(M)=D1. So existence is equivalent to existence of M such that (D2 - D1) mod 7 equals L_M mod 7.
Thus answer is YES if (diff mod 7) ∈ {0,2,3}. But need to double-check: Could there be any restriction due to the fact that the year must be non-leap? Yes, we used that February has 28 days. That's fine. But what about if we choose M = February? Then we need a non-leap year; if we inadvertently choose a leap year, February has 29 days, shift 1. But we are only considering non-leap years, so it's okay.
But wait: Could there be a case where the shift is 0,2,3 but still not possible because for some M the required D1 cannot occur in a non-leap year? For example, consider M = February (shift 0). To have Feb 1 = D1, we need Jan 1 = D1 - offset_Feb (offset_Feb = 31 mod 7 = 3). So Jan 1 = D1 - 3 mod 7. That is some day. As argued, there should exist a non-leap year with that Jan 1. But is it guaranteed? Not necessarily if that Jan 1 occurs only in leap years within the 400-year cycle. However, we need to check the distribution: Are there any days of the week that never occur as Jan 1 of a non-leap year? I doubt it. But let's verify thoroughly to avoid missing a corner.
We can analyze the Gregorian calendar 400-year cycle. Let's generate or reason: The Gregorian calendar repeats every 400 years. In that cycle, there are 97 leap years. The pattern of leap years: years divisible by 4 are leap, except centuries not divisible by 400. So within 400 years, the leap years are: all years that are multiples of 4 but not multiples of 100, plus years that are multiples of 400. That gives 100 multiples of 4, minus 4 centuries (100,200,300) that are not divisible by 400, so 100-3=97? Wait: Multiples of 4: 4,8,...,400 gives 100 numbers. Remove those that are multiples of 100 but not 400: 100,200,300 (400 is multiple of 400, so stays). So remove 3, leaving 97. Yes.
Now, the day-of-week shift from year Y to Y+1: if Y is leap, shift = 2 (since 366 mod7=2); if Y is common, shift = 1 (365 mod7=1). The day-of-week of Jan 1 for a given year can be computed given a reference.
But we want to know: For a given day D, is there at least one non-leap year in the cycle with Jan 1 = D? Possibly all days appear. But to be absolutely certain, let's simulate quickly mentally: The distribution of Jan 1 over the 400-year cycle. There's known data: In the Gregorian calendar, the probability of Jan 1 being any given day is not uniform because of the 400-year cycle, but I think each day occurs at least once for common years. Actually there is known fact: The 1st of January is a Monday, Tuesday, ..., Sunday a certain number of times in 400 years. For common years, the counts might be: 43,44,43,... something. I recall that in the Gregorian calendar, the 13th of the month occurs more on Friday? That's different. But I think for Jan 1, all days appear. Let's check quickly using known dates: e.g., Jan 1 2001 was Monday (non-leap). 2002 was Tuesday (non-leap). 2003 was Wednesday (non-leap). 2004 was Thursday (leap). 2005 was Saturday? Wait, Jan 1 2005 was Saturday? Actually let's recall: 2005 Jan 1: I think it was Saturday? Actually 2005 Jan 1: Checking: 2000 was leap, Jan 1 2000 was Saturday? Wait, known: Jan 1 2000 was Saturday. 2001 Monday, 2002 Tuesday, 2003 Wednesday, 2004 Thursday, 2005 Saturday? That would be a jump? Let's compute: 2004 is leap, so from Jan 1 2004 (Thursday) to Jan 1 2005: 2004 is leap, so add 2 => Saturday. Yes. 2006 Sunday? Actually add 1 from 2005 (common) gives Sunday. So 2006 Sunday. 2007 Monday? Actually 2006 common => +1 => Monday? Wait: 2006 Jan 1 is Sunday? Let's be systematic: Use known anchor: Jan 1 2015 was Thursday. So 2015 is non-leap. Then back compute: 2014 Jan 1: since 2014 is common, shift backward: subtract 1 => Wednesday. 2013 Jan 1: 2013 common, subtract 1 => Tuesday. 2012 leap, subtract 2 => Sunday? Actually from 2012 Jan 1 to 2013 Jan 1 is +2 (since 2012 leap), so 2012 Jan 1 = (Tuesday - 2) mod7 = Sunday? Tuesday minus 2 = Sunday (since Monday=1? Let's set numbers: assign Monday=1? But easier: Use known: Jan 1 2000 Saturday. We'll compute forward to see days for non-leap years.
Better: There is known table: For Gregorian calendar, in a 400-year cycle, the 1st of January falls on:
- Monday: 56 times? Actually I found numbers: The distribution of weekdays for Jan 1 over 400 years: Monday 56, Tuesday 55, Wednesday 58, Thursday 56, Friday 58, Saturday 56, Sunday 57? Not sure. But these numbers include both leap and common years. But we need only non-leap years.
Let's derive: The cycle of 400 years contains 97 leap years. So non-leap = 303. The distribution among weekdays for Jan 1 for non-leap years might not be exactly 303/7 = 43.285. So some days might appear 43 times, some 44. But is there any day that appears 0? Unlikely.
We can check extreme: Could Jan 1 ever be a Wednesday in a non-leap year? Yes: Jan 1 2003 was Wednesday and 2003 is non-leap. So yes. Thursday? 2004 was leap, but 2015 is Thursday non-leap. Friday? 2010? Let's recall: Jan 1 2010 was Friday? Actually 2010 Jan 1: I think it was Friday. Check: 2009 Jan 1 was Thursday (non-leap). 2010 Jan 1: since 2009 common => +1 => Friday. And 2009 was non-leap, yes. So Friday non-leap exists. Saturday? Jan 1 2005 was Saturday, 2005 non-leap. Sunday? Jan 1 2006 was Sunday, non-leap. Monday? Jan 1 2001 Monday, non-leap. Tuesday? Jan 1 2002 Tuesday, non-leap. So all days appear. So indeed for any day, there is a non-leap year with that Jan 1. So we are safe.
Thus the condition is exactly as above.
Now, check examples:
Input: monday, tuesday => D1=Monday(0), D2=Tuesday(1). diff = 1 mod7. Is 1 in {0,2,3}? No. So output NO. matches.
Input: sunday, sunday => diff=0 => yes, since February shift=0. matches.
Input: saturday, tuesday: Let's assign numbers: Monday=0, Tuesday=1, Wednesday=2, Thursday=3, Friday=4, Saturday=5, Sunday=6. Then D1=5, D2=1. diff = (1-5) mod7 = (1+7-5)=3 mod7. 3 is in set => YES. matches.
So solution seems to be: compute diff = (index(D2) - index(D1) + 7) % 7, if diff ∈ {0,2,3} print YES else NO.
But wait: Could there be also a possibility where the shift could be negative? Since we consider forward direction. The diff we computed is the forward shift from D1 to D2. But what if the month lengths mod 7 are always positive shifts forward. So we only consider that diff. That's fine.
Edge case: Could it be that the first day of the month is the given day, and the next month's first day is the given second day, but maybe the months are not necessarily in the order of increasing time? The problem says "first day of some month" and "first day of the next month". So it's sequential.
Thus answer is as above.
But I recall there is another nuance: The months have fixed lengths, but the shift from first of month M to first of month M+1 is L_M mod 7. But note: In a non-leap year, February has 28 days, which is 0 mod 7. But in a leap year, it's 1 mod 7. Since we only consider non-leap, it's 0. So shift set: {3 (Jan), 0 (Feb), 3 (Mar), 2 (Apr), 3 (May), 2 (Jun), 3 (Jul), 3 (Aug), 2 (Sep), 3 (Oct), 2 (Nov)}. That's the set. So diff must be 0,2,3.
Now, is there any other month that yields shift 1? No, only leap year February would be 1. So if diff=1, it's impossible.
What about diff=4? For example, Monday to Friday diff=4. Is there any month with L%7=4? 31%7=3,30%7=2,28%7=0. So no. So answer NO.
What about diff=5? No. diff=6? No.
Thus answer YES for diff=0,2,3; otherwise NO.
But let's double-check the months lengths: January 31 (3), February 28 (0), March 31 (3), April 30 (2), May 31 (3), June 30 (2), July 31 (3), August 31 (3), September 30 (2), October 31 (3), November 30 (2). So indeed.
Thus solution is straightforward.
But wait, the problem says "non-leap year". It does not say the year is common year; it says non-leap year. So it's fine.
Now, potential pitfalls: The input gives day names in lowercase. We'll map them to numbers.
Implementation: read two strings s1, s2. Create dict mapping day to index: e.g., days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]; index = days.index(s). Then diff = (index2 - index1) % 7. If diff in (0,2,3): print "YES" else "NO".
But careful: The mapping order: Monday is first day of week? The problem doesn't specify which day is considered first in terms of modulo arithmetic, but we only need differences modulo 7; any consistent cyclic order works. However, the week order is Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. That's the typical order, but we can just assign numbers 0..6 in that order. The difference (index2 - index1) mod 7 yields the forward shift. Since the set {0,2,3} is symmetric? Actually diff = 0,2,3; if we reversed the order (i.e., start with Sunday as 0), the differences would be different numbers modulo 7 but the condition would still be equivalent: we need diff mod 7 to be one of the values corresponding to L mod 7. However, the mapping must be consistent with the actual calendar days order. So we must use the correct order: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday as consecutive days. That is the standard Gregorian week (ISO standard Monday=1, but many countries treat Sunday as first; but the problem statement just gives day names, no indication of which is first. However, the calendar progression is linear: after Monday comes Tuesday, etc., and after Sunday comes Monday. So we must use that cyclic order. So we should map Monday->0, Tuesday->1, ..., Sunday->6. That yields correct difference. If we used different ordering, the diff might not reflect actual forward days. So we need to follow the natural order.
Thus implement as above.
Now, is there any case where diff=0,2,3 but still impossible due to year constraints? Let's consider month with shift 0: February. For February to have D1=D2, we need both first days same. Is it possible that in a non-leap year, the first of February and March are the same? Yes, as argued. But is it always possible to get any D1? If we set Jan 1 such that Feb 1 = D1, then March 1 = D1 as well. So that works for any D1.
Now, shift 2: e.g., April (30 days). April 1 -> May 1 shift 2. So if D2 = D1+2 mod7, then we need to find a year where April 1 = D1. That means Jan 1 = D1 - offset_Apr, where offset_Apr = sum of Jan+Feb+Mar days = 31+28+31=90 ≡ 6 mod7. So Jan 1 = D1 - 6 mod7. That is some day. Since Jan 1 can be any day, we can find such a non-leap year. So yes.
Shift 3: e.g., January. Jan 1 -> Feb 1 shift 3. So if D2 = D1+3 mod7, then we need a year where Jan 1 = D1. That is obviously possible. So all good.
Thus answer is correct.
But let's think if any other nuance: The problem says "during some non-leap year". That year could be any non-leap year, not necessarily the same as the current. So we can pick any year. So we are free to choose Jan 1.
Thus solution is simple.
Now, let's double-check with some edge cases: Input: sunday, wednesday? diff: Sunday (6) to Wednesday (2): (2-6) mod7 = (2+7-6)=3 => YES. Could be January? Jan 1 Sunday, Feb 1 Wednesday? Actually shift 3: if Jan1=Sunday, Jan has 31 days, so Feb1 = Sunday+31 mod7 = Sunday+3 = Wednesday. Yes, so year with Jan1=Sunday exists? Jan1 2017 was Sunday, and 2017 is non-leap? 2017 is not divisible by 4, so non-leap. Yes, it works. So example not given but should be YES.
Input: friday, monday? diff: Friday (4) to Monday (0) => (0-4) mod7 = 3? Actually (0-4) mod7 = 3? 0-4 = -4, mod7 = 3. So diff=3 => YES. Example: Could be January? Jan1=Friday? Then Feb1=Friday+3=Monday. So need a year with Jan1=Friday. Is there a non-leap year with Jan1=Friday? e.g., 2010? Jan1 2010 was Friday? Actually earlier I thought 2010 Jan1 was Friday. Let's verify: 2010 Jan1 was Friday. 2010 is non-leap (2010%4=2). Yes. So works.
Input: tuesday, saturday? diff: 1 to 5 => 4 mod7? (5-1)=4 => diff=4 not in set => NO. Check: Is there any month with shift 4? No.
Input: wednesday, tuesday? diff: 2 to 1 => (1-2)= -1 mod7 =6 => not in set => NO.
Input: thursday, thursday? diff=0 => YES (Feb). So indeed.
Thus answer.
Now, confirm problem constraints: Input is two lines, each a string from set. Output YES/NO.
Implementation: Python.
But before finalizing, I want to consider an alternative viewpoint: The problem might also consider that the months could be in reverse order? But it says "first day of some month ... while the first day of the next month ...". So it's sequential forward.
Thus solution stands.
But wait, one more nuance: The phrase "some non-leap year" might imply that we cannot arbitrarily set Jan 1; we must pick an existing year in the Gregorian calendar (which is used). But we argued there exists such a year for any desired Jan 1. However, we need to be certain that for any offset we need, there is a non-leap year with that Jan 1. We gave examples for each day of week as Jan 1 in some non-leap year between 2000 and 2020: 2001 Monday, 2002 Tuesday, 2003 Wednesday, 2005 Saturday, 2006 Sunday, 2010 Friday, 2015 Thursday. So all 7 are covered. So yes.
But what about the fact that Gregorian calendar was introduced in 1582 and the problem likely doesn't restrict to years after that; it's just the calendar system, not historical. So we assume any year that obeys the rules, even future. So any day is possible.
Thus final solution.
But wait, check if the month M must be such that the first day of that month is exactly D1. We assumed we can always set Jan 1 appropriately. However, is there any month for which the offset from Jan 1 to that month's first day is something that might not allow certain D1? Since offset is fixed, D1 = (Jan1 + offset) mod7. For a given D1, we can solve Jan1 = D1 - offset mod7. So as long as there is a non-leap year with that Jan1, we are good. Since all Jan1 days occur, it's fine.
Thus final.
Now, implement.
Potential pitfalls: mapping strings to indices. Use list of days.
Implementation details:
def solve():
days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
s1 = input().strip()
s2 = input().strip()
i1 = days.index(s1)
i2 = days.index(s2)
diff = (i2 - i1) % 7
if diff in (0,2,3):
print("YES")
else:
print("NO")
That's it.
But wait, we should also consider the possibility that M=December is not allowed because next month not in same year. But what if we considered December and January of next year? The problem says both months should belong to one year. So December cannot be used. So we only consider M=1..11.
Now, is there any month where shift is something other than 0,2,3? No.
Thus final answer.
Double-check month lengths: Actually August has 31 days, shift 3; September 30 shift 2; October 31 shift 3; November 30 shift 2; December 31 shift 3 but we don't consider. So indeed only 0,2,3.
Now, could there be a scenario where the first month is February (shift 0) and the given D1 and D2 are same? That is diff=0. So that works.
Now, think about if diff=0, it could also be achieved by any month with shift 0? Only February. So it's okay.
Now, maybe there is a nuance: The shift from month M to M+1 is L_M mod 7, but also the first day of month M+1 depends on the number of days in month M only, not on earlier months. So it's fine.
Thus answer stands.
Now, we need to produce final answer.
But the problem statement includes note: In the second sample, one can consider February 1 and March 1 of year 2015. Both these days were Sundays. Indeed, 2015: Jan1 Thursday? Let's verify: 2015 Jan1 Thursday, then Feb1 = Thursday+31 mod7 = Thursday+3 = Sunday? Actually Thursday (3) +3 = Sunday (6). Yes, Sunday. March1 = Feb1+28 = Sunday+0 = Sunday. So diff=0. So that's consistent.
Third sample: Saturday to Tuesday diff=3. They mention July 1 and August 1 of year 2017. 2017 Jan1 Sunday. Let's compute offsets: July is month 7. Offset to July1: sum Jan-Jun = 31+28+31+30+31+30 = 181? Actually 31+28=59, +31=90, +30=120, +31=151, +30=181. 181 mod7 = 6 (since 175=25*7, remainder 6). So July1 = Jan1+6 = Sunday+6 = Saturday. Good. August1 = July1+31 = Saturday+3 = Tuesday. Yes.
So all good.
Now, check if any other scenarios: For example, Monday to Wednesday diff=2. That could be April to May? If April1 Monday, then May1 Wednesday (since 30 days shift 2). So need a year where April1 Monday. That would require Jan1 = Monday - offset_Apr (offset_Apr=6) = Tuesday? Actually Monday -6 = Tuesday? Monday (0) -6 mod7 = 1 (Tuesday). So Jan1 = Tuesday. Is there a non-leap year with Jan1 Tuesday? 2002 Jan1 Tuesday. So yes. So that's possible.
Thus solution is correct.
Now, possible edge: Could the first month be February and the second be March, but if the year is non-leap, shift 0. So D2 must equal D1. If the input gives two different days, diff not 0, then February is not valid. So we need to check other months.
Now, what about the possibility that we could consider the first month being a month where the shift is 2 or 3 but due to the offset for that month from Jan1, the required D1 may be impossible? Actually we argued it's possible because we can set Jan1 accordingly. But is it always possible to have Jan1 be any day in a non-leap year? Yes, as we have examples for all. So fine.
But wait, the offset for the month we pick is fixed, so the required Jan1 is D1 minus offset mod7. If that Jan1 day occurs in a non-leap year, we are good. Since all Jan1 days occur in some non-leap year, it's always possible. But is it possible that the required Jan1 occurs only in leap years? That would be problematic. But we can check: For each day of week, there is at least one non-leap year with that Jan1. Let's verify systematically: Over the 400-year cycle, count non-leap years with Jan1 = Monday, etc. I can quickly approximate: For any given day, it's likely at least 43 times. But is there any day that never appears as Jan1 in a non-leap year? I think not. Let's confirm by constructing a simple argument: The sequence of Jan1 days for consecutive years (including leap and non-leap) cycles through all possibilities because the total shift over 400 years is 0 mod7, and the pattern includes both leap and non-leap. If a particular day never appeared as Jan1 of a non-leap year, then that day would only appear as Jan1 of leap years. But there are only 97 leap years; could they cover all 7 days? Possibly yes, but that would mean each day appears as Jan1 of some leap year. But does that preclude a day from appearing as Jan1 of a non-leap year? Not necessarily; a day could appear both in leap and non-leap years. But could a day appear only in leap years? Let's check: For a given day D, consider the years in the 400-year cycle. Jan1 day changes by 1 or 2 each year. The pattern of leap years is known. I can test a specific day: e.g., Wednesday. We already have 2003 (non-leap) with Jan1 Wednesday. So that's covered. Similarly all days we found examples. So it's safe.
Thus final answer.
Now, the solution is extremely simple. Probably the problem is from Codeforces problem "Days of Week" (maybe 287A? Actually I recall a problem 104A? Not sure). But it's known.
Now, I'll craft a final response: explanation of reasoning, then code.
But the instruction: "You are given names of two days of the week. Please, determine whether it is possible ..." So I need to output answer.
As ChatGPT, I need to produce final answer. I should output the code? The user likely expects the solution. The conversation might be from a programming problem, so I need to provide code. The instruction says: "The input consists of two lines... Output Print 'YES' or 'NO'." So it's a programming problem. I should provide a solution.
Thus I'll write a Python solution.
But also note that the problem might be from some contest where they ask to just output YES/NO, but I'm ChatGPT, I can produce the code.
Now, I'll include explanation? The user asked: "You are given names of two days of the week. Please, determine whether it is possible..." This is a problem statement, and they want answer? Possibly they want the solution code. I think I should output the code.
Given the format, I'll produce a Python solution with a solve() function.
But to be safe, I'll output the final answer: "YES" or "NO"? No, the user didn't give specific input; they gave the problem statement. Usually in such interactions, they ask to write a solution. So I'll write a code.
Thus final output: Python code.
But the instruction: "You are given names... Input... Output...". It's typical problem description. The user might want me to write a program that reads input and prints output. So I'll produce that.
Thus final answer: Python code.
Potential edge: Input lines may have trailing spaces. Use .strip().
Now, final code.
Also, note that the problem might be solved by checking diff mod 7 in set {0,2,3}.
Thus final.
Now, I'll write final answer. ```python
def main():
# mapping day names to numbers (0 = Monday, ..., 6 = Sunday)
days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
s1 = input().strip().lower()
s2 = input().strip().lower()
d1 = days.index(s1)
d2 = days.index(s2)
diff = (d2 - d1) % 7
if diff in (0, 2, 3):
print("YES")
else:
print("NO")
if name == "main":
main()