/*
题意:程序从1到n版本升级,正版+正版->正版,正版+盗版->盗版,盗版+盗版->盗版
正版+破解版->正版,盗版+破解版->盗版
DP:每种情况考虑一遍,递推就行了
注意:开long long
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f;
struct Node
{
int u, v, w;
char s[];
}node[MAXN];
long long dp[MAXN][]; bool cmp(Node x, Node y)
{
if (x.u == y.u) return x.v < y.v;
return x.u < y.u;
} int main(void) //URAL 1741 Communication Fiend
{
//freopen ("T.in", "r", stdin); int n, m;
while (scanf ("%d%d", &n, &m) == )
{
for (int i=; i<=m; ++i)
{
scanf ("%d %d %d %s", &node[i].u, &node[i].v, &node[i].w, &node[i].s);
}
sort (node+, node++m, cmp); memset (dp, -, sizeof (dp));
dp[][] = ;
for (int i=; i<=m; ++i)
{
Node a = node[i];
if (a.s[] == 'L')
{
if (dp[a.u][] != - && (dp[a.v][] == - || dp[a.v][] > dp[a.u][] + a.w))
{
dp[a.v][] = dp[a.u][] + a.w;
}
}
else if (a.s[] == 'P')
{
if (dp[a.u][] != - && (dp[a.v][] == - || dp[a.v][] > dp[a.u][] + a.w))
{
dp[a.v][] = dp[a.u][] + a.w;
}
if (dp[a.u][] != - && (dp[a.v][] == - || dp[a.v][] > dp[a.u][] + a.w))
{
dp[a.v][] = dp[a.u][] + a.w;
}
}
else if (a.s[] == 'C')
{
if (dp[a.u][] != - && (dp[a.v][] == - || dp[a.v][] > dp[a.u][] + a.w))
{
dp[a.v][] = dp[a.u][] + a.w;
}
if (dp[a.u][] != - && (dp[a.v][] == - || dp[a.v][] > dp[a.u][] + a.w))
{
dp[a.v][] = dp[a.u][] + a.w;
}
}
} if (dp[n][] == - && dp[n][] == -) puts ("Offline");
else
{
puts ("Online");
if (dp[n][] == -) printf ("%I64d\n", dp[n][]);
else if (dp[n][] == -) printf ("%I64d\n", dp[n][]);
else printf ("%I64d\n", min (dp[n][], dp[n][]));
}
} return ;
} /*
Online
Offline
*/
/*
WA了4,5发,第一是抄Kuangbin的模板抄错了;第二是选取的INF值太小了
以后学习一下最值的选取的方法:)
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
using namespace std; const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f;
const int INFF = 0x7fffffff;
const long long inf = ((long long)<<);
struct Edge
{
int v, w;
int f;
Edge (int _v = , int _w = , int _f = ) : v (_v), w (_w), f (_f) {}
};
bool vis[MAXN];
int cnt[MAXN];
long long d[MAXN][];
vector<Edge> G[MAXN];
int n, m; void add_edge(int u, int v, int w, int f)
{
G[u].push_back (Edge (v, w, f));
} bool SPFA(int s)
{
memset (cnt, , sizeof (cnt));
memset (vis, false, sizeof (vis));
for (int i=; i<=n; ++i) d[i][] = d[i][] = inf;
vis[s] = true; d[s][] = ; cnt[s] = ; queue<int> Q; Q.push (s);
while (!Q.empty ())
{
int u = Q.front (); Q.pop ();
vis[u] = false; //失误!
for (int i=; i<G[u].size (); ++i)
{
int v = G[u][i].v; int w = G[u][i].w; int f = G[u][i].f;
if (f == )
{
if (d[v][] > d[u][] + w) d[v][] = d[u][] + w;
}
else if (f == )
{
if (d[v][] > d[u][] + w) d[v][] = d[u][] + w;
if (d[v][] > d[u][] + w) d[v][] = d[u][] + w;
}
else
{
if (d[v][] > d[u][] + w) d[v][] = d[u][] + w;
if (d[v][] > d[u][] + w) d[v][] = d[u][] + w;
}
if (!vis[v])
{
vis[v] = true; Q.push (v);
if (++cnt[v] > n) return true;
}
}
} return false;
} int main(void) //URAL 1741 Communication Fiend
{
//freopen ("T.in", "r", stdin); while (scanf ("%d%d", &n, &m) == )
{
for (int i=; i<=n; ++i) G[i].clear ();
for (int i=; i<=m; ++i)
{
int u, v, w, f; char s[];
scanf ("%d %d %d %s", &u, &v, &w, &s);
if (s[] == 'L') f = ;
else if (s[] == 'P') f = ;
else if (s[] == 'C') f = -;
add_edge (u, v, w, f);
} if (!SPFA ())
{
long long ans = min (d[n][], d[n][]);
if (ans == inf) puts ("Offline");
else {puts ("Online"); printf ("%I64d\n", ans);}
}
} return ;
} /*
Online
Offline
*/
SPFA